0N/A/*
6447N/A * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Aimport java.util.*;
0N/Aimport java.io.*;
0N/Aimport java.nio.charset.*;
0N/Aimport java.text.MessageFormat;
1696N/Aimport java.util.logging.Level;
1696N/Aimport java.util.logging.Logger;
0N/A
0N/Apublic class WrapperGenerator {
0N/A /* XLibParser converts Xlib.h to a Java Object that encapsulates the
0N/A * X11 API and data structures */
0N/A // Charset and decoder for ISO-8859-15
0N/A private final static Logger log = Logger.getLogger("WrapperGenerator");
0N/A boolean generateLog = true;
0N/A boolean wide;
0N/A private static Charset charset = Charset.forName("ISO-8859-15");
0N/A
0N/A String package_name = "sun.awt.X11";
0N/A String package_path = "sun/awt/X11";
0N/A String sizerFileName = "sizer.c";
0N/A String defaultBaseClass = "XWrapperBase";
0N/A
0N/A String compile_options = "-lX11";
0N/A static Hashtable symbolTable = new Hashtable();
0N/A static Hashtable sizeTable32bit = new Hashtable();
0N/A static Hashtable sizeTable64bit = new Hashtable();
0N/A static Hashtable knownSizes32 = new Hashtable();
0N/A static Hashtable knownSizes64 = new Hashtable();
0N/A static {
0N/A/*
0N/A knownSizes64.put("", Integer.valueOf());
0N/A knownSizes32.put("", Integer.valueOf());
0N/A*/
0N/A knownSizes64.put("XComposeStatus", Integer.valueOf(16));
0N/A knownSizes64.put("XTimeCoord", Integer.valueOf(16));
0N/A knownSizes64.put("XExtData", Integer.valueOf(32));
0N/A knownSizes64.put("XWindowChanges", Integer.valueOf(40));
0N/A knownSizes64.put("XOMCharSetList", Integer.valueOf(16));
0N/A knownSizes64.put("XModifierKeymap", Integer.valueOf(16));
0N/A knownSizes32.put("XIMValuesList", Integer.valueOf(8));
0N/A knownSizes32.put("XGCValues", Integer.valueOf(92));
0N/A// knownSizes32.put("XIMStringConversionCallbackStruct", Integer.valueOf(16));
0N/A }
0N/A
0N/A private static abstract class BaseType {
0N/A
0N/A String real_type;
0N/A String name;
0N/A
0N/A
0N/A public String getName() {
0N/A return name;
0N/A }
0N/A public String getRealType() {
0N/A return real_type;
0N/A }
0N/A
0N/A public String toString() {
0N/A return name;
0N/A }
0N/A }
0N/A
0N/A private static class AtomicType extends BaseType {
0N/A
0N/A private boolean alias;
0N/A private String aliasName;
0N/A
0N/A static final int TYPE_INT=0;
0N/A static final int TYPE_CHAR=1;
0N/A static final int TYPE_LONG=2;
0N/A static final int TYPE_LONG_LONG=3;
0N/A static final int TYPE_DOUBLE=4;
0N/A static final int TYPE_FLOAT=5;
0N/A static final int TYPE_PTR=6;
0N/A static final int TYPE_SHORT=7;
0N/A static final int TYPE_BOOL = 8;
0N/A static final int TYPE_STRUCT = 9;
0N/A static final int TYPE_ARRAY = 10;
0N/A static final int TYPE_BYTE=11;
0N/A static final int TYPE_ATOM = 12;
0N/A static final int TYPE_ULONG = 13;
0N/A static int getTypeForString(String str) {
0N/A int type=-1;
0N/A if (str.equals("int"))
0N/A type = AtomicType.TYPE_INT;
0N/A else if (str.equals("long"))
0N/A type = AtomicType.TYPE_LONG;
0N/A else if (str.equals("byte"))
0N/A type = AtomicType.TYPE_BYTE;
0N/A else if (str.equals("char"))
0N/A type = AtomicType.TYPE_CHAR;
0N/A else if (str.equals("long long"))
0N/A type = AtomicType.TYPE_LONG_LONG;
0N/A else if (str.equals("double"))
0N/A type = AtomicType.TYPE_DOUBLE;
0N/A else if (str.equals("float"))
0N/A type = AtomicType.TYPE_FLOAT;
0N/A else if (str.equals("pointer"))
0N/A type = AtomicType.TYPE_PTR;
0N/A else if (str.equals("short"))
0N/A type = AtomicType.TYPE_SHORT;
0N/A else if (str.equals("Bool"))
0N/A type = AtomicType.TYPE_BOOL;
0N/A else if (str.equals("struct"))
0N/A type = AtomicType.TYPE_STRUCT;
0N/A else if (str.equals("Atom"))
0N/A type = AtomicType.TYPE_ATOM;
0N/A else if (str.equals("array"))
0N/A type = TYPE_ARRAY;
0N/A else if (str.equals("ulong"))
0N/A type = TYPE_ULONG;
0N/A else throw new IllegalArgumentException("Uknown type string: " + str);
0N/A
0N/A return type;
0N/A }
0N/A String getJavaType() {
0N/A if (referencedType != null) {
0N/A if (referencedType instanceof AtomicType) {
0N/A return ((AtomicType)referencedType).getJavaType();
0N/A } else {
0N/A return referencedType.getName();
0N/A }
0N/A } else {
0N/A return getJavaTypeForType(type);
0N/A }
0N/A }
0N/A static String getJavaTypeForType(int type) {
0N/A switch (type) {
0N/A case TYPE_INT:
0N/A return "int";
0N/A case TYPE_CHAR:
0N/A return "char";
0N/A case TYPE_BYTE:
0N/A return "byte";
0N/A case TYPE_LONG:
0N/A case TYPE_LONG_LONG:
0N/A case TYPE_PTR:
0N/A case TYPE_ULONG:
0N/A return "long";
0N/A case TYPE_DOUBLE:
0N/A return "double";
0N/A case TYPE_FLOAT:
0N/A return "float";
0N/A case TYPE_SHORT:
0N/A return "short";
0N/A case TYPE_BOOL:
0N/A return "boolean";
0N/A case TYPE_ATOM:
0N/A return "long";
0N/A default:
0N/A throw new IllegalArgumentException("Unknown type: " + type);
0N/A }
0N/A }
0N/A String getItemSize() {
0N/A if (referencedType != null) {
0N/A if (referencedType instanceof StructType) {
0N/A return ((StructType)referencedType).getSize();
0N/A } else {
0N/A return ((AtomicType)referencedType).getItemSize();
0N/A }
0N/A } else {
0N/A int i32 = getNativeSizeForAccess(getJavaAccess(false));
0N/A int i64 = getNativeSizeForAccess(getJavaAccess(true));
0N/A if (i32 != i64) {
0N/A return "Native.get" + getNativeAccess() + "Size()";
0N/A } else {
0N/A return Integer.toString(i32);
0N/A }
0N/A }
0N/A }
0N/A
0N/A String getJavaResult(String offset, String base) {
0N/A String res = null;
0N/A switch (type) {
0N/A case TYPE_STRUCT:
0N/A res = "pData + " + offset;
0N/A break;
0N/A case TYPE_PTR:
0N/A if (referencedType == null || referencedType instanceof StructType) {
0N/A res = base + "+" + offset;
0N/A } else if (referencedType instanceof AtomicType) {
0N/A res = MessageFormat.format("Native.get{0}({1})",
0N/A new Object[] {getNativeAccessForType(((AtomicType)referencedType).type),
0N/A base + "+" + offset});
0N/A }
0N/A break;
0N/A case TYPE_ARRAY:
0N/A if (referencedType instanceof StructType) {
0N/A res = "pData + " + offset;
0N/A } else if (referencedType instanceof AtomicType) {
0N/A res = MessageFormat.format("Native.get{0}(pData + {1})",
0N/A new Object[] {getNativeAccessForType(((AtomicType)referencedType).type),
0N/A offset});
0N/A }
0N/A break;
0N/A default:
0N/A res = MessageFormat.format("(Native.get{0}(pData+{1}))",
0N/A new Object[] {getNativeAccess(), offset});
0N/A }
0N/A return getJavaResultConversion(res, base);
0N/A }
0N/A String getJavaResultConversion(String value, String base) {
0N/A if (referencedType != null) {
0N/A if (referencedType instanceof StructType) {
0N/A if (type == TYPE_PTR) {
0N/A return MessageFormat.format("({2} != 0)?(new {0}({1})):(null)", new Object[] {referencedType.getName(),value, base});
0N/A } else {
0N/A return MessageFormat.format("new {0}({1})", new Object[] {referencedType.getName(),value});
0N/A }
0N/A } else {
0N/A return value;
0N/A }
0N/A } else {
0N/A return getJavaResultConversionForType(type, value);
0N/A }
0N/A }
0N/A static String getJavaResultConversionForType(int type, String value) {
0N/A return value;
0N/A }
0N/A String getNativeAccess() {
0N/A return getNativeAccessForType(type);
0N/A }
0N/A String getJavaAccess(boolean wide) {
0N/A return getJavaAccessForType(type, wide);
0N/A }
0N/A static String getJavaAccessForType(int type, boolean wide) {
0N/A switch (type) {
0N/A case TYPE_INT:
0N/A return "Int";
0N/A case TYPE_CHAR:
0N/A return "Char";
0N/A case TYPE_BYTE:
0N/A return "Byte";
0N/A case TYPE_LONG:
0N/A case TYPE_PTR:
0N/A case TYPE_ARRAY:
0N/A case TYPE_STRUCT:
0N/A case TYPE_ATOM:
0N/A return (wide?"Long":"Int");
0N/A case TYPE_LONG_LONG:
0N/A return "Long";
0N/A case TYPE_ULONG:
0N/A return (wide?"ULong":"UInt");
0N/A case TYPE_DOUBLE:
0N/A return "Double";
0N/A case TYPE_FLOAT:
0N/A return "Float";
0N/A case TYPE_SHORT:
0N/A return "Short";
0N/A case TYPE_BOOL:
0N/A return "Int";
0N/A default:
0N/A throw new IllegalArgumentException("Unknown type: " + type);
0N/A }
0N/A }
0N/A static String getNativeAccessForType(int type) {
0N/A switch (type) {
0N/A case TYPE_INT:
0N/A return "Int";
0N/A case TYPE_CHAR:
0N/A return "Char";
0N/A case TYPE_BYTE:
0N/A return "Byte";
0N/A case TYPE_LONG:
0N/A case TYPE_PTR:
0N/A case TYPE_ARRAY:
0N/A case TYPE_STRUCT:
0N/A return "Long";
0N/A case TYPE_LONG_LONG:
0N/A return "Long";
0N/A case TYPE_ULONG:
0N/A return "ULong";
0N/A case TYPE_DOUBLE:
0N/A return "Double";
0N/A case TYPE_FLOAT:
0N/A return "Float";
0N/A case TYPE_SHORT:
0N/A return "Short";
0N/A case TYPE_BOOL:
0N/A return "Bool";
0N/A case TYPE_ATOM:
0N/A return "Long";
0N/A default:
0N/A throw new IllegalArgumentException("Unknown type: " + type);
0N/A }
0N/A }
0N/A
0N/A static int getNativeSizeForAccess(String access) {
0N/A if (access.equals("Int")) return 4;
0N/A else if (access.equals("Byte")) return 1;
0N/A else if (access.equals("Long")) return 8;
0N/A else if (access.equals("Double")) return 8;
0N/A else if (access.equals("Float")) return 4;
0N/A else if (access.equals("Char")) return 2;
0N/A else if (access.equals("Short")) return 2;
0N/A else if (access.equals("ULong")) return 8;
0N/A else if (access.equals("UInt")) return 4;
0N/A else throw new IllegalArgumentException("Unknow access type: " + access);
0N/A }
0N/A
0N/A String getJavaConversion(String offset, String value) {
0N/A if (referencedType != null) {
0N/A if (referencedType instanceof StructType) {
0N/A return getJavaConversionForType(TYPE_PTR, offset, value + ".pData");
0N/A } else {
0N/A if (type == TYPE_ARRAY) {
0N/A return getJavaConversionForType(((AtomicType)referencedType).type, offset, value);
0N/A } else { // TYPE_PTR
0N/A return getJavaConversionForType(TYPE_PTR, offset, value);
0N/A }
0N/A }
0N/A } else {
0N/A return getJavaConversionForType(type, offset, value);
0N/A }
0N/A }
0N/A static String getJavaConversionForType(int type, String offset, String value) {
0N/A return MessageFormat.format("Native.put{0}({2}, {1})", new Object[] {getNativeAccessForType(type), value, offset});
0N/A }
0N/A
0N/A
0N/A int type;
0N/A int offset;
0N/A int direction;
0N/A BaseType referencedType;
0N/A int arrayLength = -1;
0N/A boolean autoFree = false;
0N/A public AtomicType(int _type,String _name, String _real_type) {
0N/A name = _name.replaceAll("[* \t]","");
0N/A if ((name.indexOf("[") != -1) || (name.indexOf("]") != -1))
0N/A {
0N/A name = name.replaceAll("\\[.*\\]","");
0N/A }
0N/A type = _type;
0N/A real_type = _real_type;
0N/A if (real_type == null)
0N/A {
0N/A System.out.println(" real type is null");
0N/A
0N/A }
0N/A }
0N/A public boolean isIn() {
0N/A return direction == 0;
0N/A }
0N/A public boolean isOut() {
0N/A return direction == 1;
0N/A }
0N/A public boolean isInOut() {
0N/A return direction == 2;
0N/A }
0N/A public boolean isAutoFree() {
0N/A return autoFree;
0N/A }
0N/A public void setAttributes(String[] attributes) {
0N/A String mod = attributes[3];
0N/A if ("in".equals(mod)) {
0N/A direction = 0;
0N/A } else if ("out".equals(mod)) {
0N/A direction = 1;
0N/A if (attributes.length > 4 && "free".equals(attributes[4])) {
0N/A autoFree = true;
0N/A }
0N/A } else if ("inout".equals(mod)) {
0N/A direction = 2;
0N/A } else if ("alias".equals(mod)) {
0N/A alias = true;
0N/A aliasName = attributes[4];
0N/A } else if (type == TYPE_ARRAY || type == TYPE_PTR || type == TYPE_STRUCT) {
0N/A referencedType = (BaseType)symbolTable.get(mod);
0N/A if (referencedType == null) {
0N/A log.warning("Can't find type for name " + mod);
0N/A }
0N/A if (attributes.length > 4) { // array length
0N/A try {
0N/A arrayLength = Integer.parseInt(attributes[4]);
0N/A } catch (Exception e) {
0N/A }
0N/A }
0N/A }
0N/A }
0N/A public BaseType getReferencedType() {
0N/A return referencedType;
0N/A }
0N/A public int getArrayLength() {
0N/A return arrayLength;
0N/A }
0N/A public void setOffset(int o)
0N/A {
0N/A offset = o;
0N/A }
0N/A public int getType() {
0N/A return type;
0N/A }
0N/A public String getTypeUpperCase() {
0N/A switch (type) {
0N/A case TYPE_INT:
0N/A return "Int";
0N/A case TYPE_CHAR:
0N/A return "Char";
0N/A case TYPE_BYTE:
0N/A return "Byte";
0N/A case TYPE_LONG:
0N/A case TYPE_LONG_LONG:
0N/A case TYPE_PTR:
0N/A return "Long";
0N/A case TYPE_DOUBLE:
0N/A return "Double";
0N/A case TYPE_FLOAT:
0N/A return "Float";
0N/A case TYPE_SHORT:
0N/A return "Short";
0N/A case TYPE_BOOL:
0N/A return "Int";
0N/A case TYPE_ATOM:
0N/A return "Long";
0N/A case TYPE_ULONG:
0N/A return "ULong";
0N/A default: throw new IllegalArgumentException("Uknown type");
0N/A }
0N/A }
0N/A public int getOffset()
0N/A {
0N/A return offset;
0N/A }
0N/A public boolean isAlias() {
0N/A return alias;
0N/A }
0N/A public String getAliasName() {
0N/A return aliasName;
0N/A }
0N/A }
0N/A
0N/A private static class StructType extends BaseType {
0N/A
0N/A Vector members;
0N/A String description;
0N/A boolean packed;
0N/A int size;
0N/A String baseClass, interfaces;
0N/A boolean isInterface;
0N/A String javaClassName;
0N/A
0N/A /**
0N/A * Construct new structured type.
0N/A * Description is used for name and type definition and has the following format:
0N/A * structName [ '[' base classe ']' ] [ '{' interfaces '}' ] [ '|' javaClassName ]
0N/A */
0N/A public StructType(String _desc)
0N/A {
0N/A members = new Vector();
0N/A parseDescription(_desc);
0N/A }
0N/A public int getNumFields()
0N/A {
0N/A return members.size();
0N/A }
0N/A public void setName(String _name)
0N/A {
0N/A _name = _name.replaceAll("[* \t]","");
0N/A parseDescription(_name);
0N/A }
0N/A
0N/A public void setSize(int i)
0N/A {
0N/A size = i;
0N/A }
0N/A
0N/A public String getDescription()
0N/A {
0N/A return description;
0N/A }
0N/A
0N/A public Enumeration getMembers()
0N/A {
0N/A return members.elements();
0N/A }
0N/A
0N/A public void addMember(BaseType tp)
0N/A {
0N/A members.add(tp);
0N/A }
0N/A public String getBaseClass() {
0N/A return baseClass;
0N/A }
0N/A public String getInterfaces() {
0N/A return interfaces;
0N/A }
0N/A public boolean getIsInterface() {
0N/A return isInterface;
0N/A }
0N/A public String getJavaClassName() {
0N/A return javaClassName;
0N/A }
0N/A void parseDescription(String _desc) {
0N/A if (_desc.indexOf('[') != -1) { // Has base class
0N/A baseClass = _desc.substring(_desc.indexOf('[')+1, _desc.indexOf(']'));
0N/A _desc = _desc.substring(0, _desc.indexOf('[')) + _desc.substring(_desc.indexOf(']')+1);
0N/A }
0N/A if (_desc.indexOf('{') != -1) { // Has base class
0N/A interfaces = _desc.substring(_desc.indexOf('{')+1, _desc.indexOf('}'));
0N/A _desc = _desc.substring(0, _desc.indexOf('{')) + _desc.substring(_desc.indexOf('}')+1);
0N/A }
0N/A if (_desc.startsWith("-")) { // Interface
0N/A isInterface = true;
0N/A _desc = _desc.substring(1, _desc.length());
0N/A }
0N/A if (_desc.indexOf("|") != -1) {
0N/A javaClassName = _desc.substring(_desc.indexOf('|')+1, _desc.length());
0N/A _desc = _desc.substring(0, _desc.indexOf('|'));
0N/A }
0N/A name = _desc;
0N/A if (javaClassName == null) {
0N/A javaClassName = name;
0N/A }
0N/A description = _desc;
0N/A// System.out.println("Struct " + name + " extends " + baseClass + " implements " + interfaces);
0N/A }
0N/A
0N/A /**
0N/A * Returns String containing Java code calculating size of the structure depending on the data model
0N/A */
0N/A public String getSize() {
0N/A String s32 = (String) WrapperGenerator.sizeTable32bit.get(getName());
0N/A String s64 = (String) WrapperGenerator.sizeTable64bit.get(getName());
0N/A if (s32 == null || s64 == null) {
0N/A return (s32 == null)?(s64):(s32);
0N/A }
0N/A if (s32.equals(s64)) {
0N/A return s32;
0N/A } else {
0N/A return MessageFormat.format("((XlibWrapper.dataModel == 32)?({0}):({1}))", new Object[] {s32, s64});
0N/A }
0N/A }
0N/A public String getOffset(AtomicType atp) {
0N/A String key = getName()+"."+(atp.isAlias() ? atp.getAliasName() : atp.getName());
0N/A String s64 = (String) WrapperGenerator.sizeTable64bit.get(key);
0N/A String s32 = (String) WrapperGenerator.sizeTable32bit.get(key);
0N/A if (s32 == null || s64 == null) {
0N/A return (s32 == null)?(s64):(s32);
0N/A }
0N/A if (s32.equals(s64)) {
0N/A return s32;
0N/A } else {
0N/A return MessageFormat.format("((XlibWrapper.dataModel == 32)?({0}):({1}))", new Object[]{s32, s64});
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static class FunctionType extends BaseType {
0N/A
0N/A Vector args;
0N/A String description;
0N/A boolean packed;
0N/A String returnType;
0N/A
0N/A int alignment;
0N/A
0N/A public FunctionType(String _desc)
0N/A {
0N/A args = new Vector();
0N/A description = _desc;
0N/A setName(_desc);
0N/A }
0N/A boolean isVoid() {
0N/A return (returnType == null);
0N/A }
0N/A String getReturnType() {
0N/A if (returnType == null) {
0N/A return "void";
0N/A } else {
0N/A return returnType;
0N/A }
0N/A }
0N/A
0N/A public int getNumArgs()
0N/A {
0N/A return args.size();
0N/A }
0N/A public void setName(String _name)
0N/A {
0N/A if (_name.startsWith("!")) {
0N/A _name = _name.substring(1, _name.length());
0N/A }
0N/A if (_name.indexOf("|") != -1) {
0N/A returnType = _name.substring(_name.indexOf("|")+1, _name.length());
0N/A _name = _name.substring(0, _name.indexOf("|"));
0N/A }
0N/A name = _name.replaceAll("[* \t]","");
0N/A }
0N/A
0N/A public String getDescription()
0N/A {
0N/A return description;
0N/A }
0N/A
0N/A public Collection getArguments()
0N/A {
0N/A return args;
0N/A }
0N/A public void addArgument(BaseType tp)
0N/A {
0N/A args.add(tp);
0N/A }
0N/A }
0N/A
0N/A public String makeComment(String str)
0N/A {
0N/A StringTokenizer st = new StringTokenizer(str,"\r\n");
0N/A String ret="";
0N/A
0N/A while (st.hasMoreTokens())
0N/A {
0N/A ret = ret + "//" + st.nextToken() + "\n";
0N/A }
0N/A
0N/A return ret;
0N/A }
0N/A
0N/A public String getJavaTypeForSize(int size) {
0N/A switch(size) {
0N/A case 1: return "byte";
0N/A case 2: return "short";
0N/A case 4: return "int";
0N/A case 8: return "long";
0N/A default: throw new RuntimeException("Unsupported size: " + size);
0N/A }
0N/A }
0N/A public String getOffsets(StructType stp,AtomicType atp, boolean wide)
0N/A {
0N/A String key = stp.getName()+"."+atp.getName();
0N/A return wide == true ? (String) sizeTable64bit.get(key) : (String) sizeTable32bit.get(key);
0N/A }
0N/A
0N/A public String getStructSize(StructType stp, boolean wide)
0N/A {
0N/A return wide == true ? (String) sizeTable64bit.get(stp.getName()) : (String) sizeTable32bit.get(stp.getName());
0N/A }
0N/A
0N/A public int getLongSize(boolean wide)
0N/A {
0N/A return Integer.parseInt(wide == true ? (String)sizeTable64bit.get("long") : (String)sizeTable32bit.get("long"));
0N/A }
0N/A
0N/A public int getPtrSize(boolean wide)
0N/A {
0N/A return Integer.parseInt(wide == true ? (String)sizeTable64bit.get("ptr") : (String)sizeTable32bit.get("ptr"));
0N/A }
0N/A public int getBoolSize(boolean wide) {
0N/A return getOrdinalSize("Bool", wide);
0N/A }
0N/A public int getOrdinalSize(String ordinal, boolean wide) {
0N/A return Integer.parseInt(wide == true ? (String)sizeTable64bit.get(ordinal) : (String)sizeTable32bit.get(ordinal));
0N/A }
0N/A
0N/A public void writeToString(StructType stp, PrintWriter pw) {
0N/A int type;
0N/A pw.println("\n\n\tString getName() {\n\t\treturn \"" + stp.getName()+ "\"; \n\t}");
4544N/A pw.println("\n\n\tString getFieldsAsString() {\n\t\tStringBuilder ret = new StringBuilder(" + stp.getNumFields() * 40 + ");\n");
0N/A
0N/A for (Enumeration e = stp.getMembers() ; e.hasMoreElements() ;) {
0N/A AtomicType tp = (AtomicType) e.nextElement();
0N/A
0N/A type = tp.getType();
0N/A String name = tp.getName().replace('.', '_');
0N/A if ((name != null) && (name.length() > 0))
0N/A {
0N/A if (type == AtomicType.TYPE_ATOM) {
4544N/A pw.println("\t\tret.append(\"" + name + " = \" ).append( XAtom.get(get_" + name + "()) ).append(\", \");");
0N/A } else if (name.equals("type")) {
4544N/A pw.println("\t\tret.append(\"type = \").append( XlibWrapper.eventToString[get_type()] ).append(\", \");");
0N/A } else if (name.equals("window")){
4544N/A pw.println("\t\tret.append(\"window = \" ).append( getWindow(get_window()) ).append(\", \");");
0N/A } else if (type == AtomicType.TYPE_ARRAY) {
4544N/A pw.print("\t\tret.append(\"{\")");
0N/A for (int i = 0; i < tp.getArrayLength(); i++) {
4544N/A pw.print("\n\t\t.append( get_" + name + "(" + i + ") ).append(\" \")");
0N/A }
4544N/A pw.println(".append( \"}\");");
0N/A } else {
4544N/A pw.println("\t\tret.append(\"" + name +" = \").append( get_"+ name+"() ).append(\", \");");
0N/A }
0N/A }
0N/A
0N/A }
4544N/A pw.println("\t\treturn ret.toString();\n\t}\n\n");
0N/A }
0N/A
0N/A public void writeStubs(StructType stp, PrintWriter pw) {
0N/A int type;
0N/A String prefix = "";
0N/A if (!stp.getIsInterface()) {
0N/A prefix = "\t\tabstract ";
0N/A } else {
0N/A prefix = "\t";
0N/A }
0N/A for (Enumeration e = stp.getMembers() ; e.hasMoreElements() ;) {
0N/A AtomicType tp = (AtomicType) e.nextElement();
0N/A
0N/A type = tp.getType();
0N/A String name = tp.getName().replace('.','_');
0N/A if ((name != null) && (name.length() > 0))
0N/A {
0N/A if (type == AtomicType.TYPE_ARRAY) {
0N/A // Returns pointer to the start of the array
0N/A pw.println(prefix + "long get_" +name +"();");
0N/A
0N/A pw.println(prefix + tp.getJavaType() + " get_" +name +"(int index);");
0N/A pw.println(prefix + "void set_" +name +"(int index, " + tp.getJavaType() + " v);");
0N/A } else {
0N/A pw.println(prefix + tp.getJavaType() + " get_" +name +"();");
0N/A if (type != AtomicType.TYPE_STRUCT) pw.println(prefix + "void set_" +name +"(" + tp.getJavaType() + " v);");
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A private int padSize(int size, int wordLength) {
0N/A int bytesPerWord = wordLength / 8;
0N/A // Make size dividable by bytesPerWord
0N/A return (size + bytesPerWord / 2) / bytesPerWord * bytesPerWord;
0N/A }
0N/A
0N/A public void writeAccessorImpls(StructType stp, PrintWriter pw) {
0N/A int type;
0N/A int i=0;
0N/A String s_size_32 = getStructSize(stp, false);
0N/A String s_size_64 = getStructSize(stp, true);
0N/A int acc_size_32 = 0;
0N/A int acc_size_64 = 0;
0N/A String s_log = (generateLog?"log.finest(\"\");":"");
0N/A for (Enumeration e = stp.getMembers() ; e.hasMoreElements() ;) {
0N/A AtomicType tp = (AtomicType) e.nextElement();
0N/A
0N/A type = tp.getType();
0N/A String name = tp.getName().replace('.','_');
0N/A String pref = "\tpublic " ;
0N/A if ((name != null) && (name.length() > 0))
0N/A {
0N/A String jt = tp.getJavaType();
0N/A String ja_32 = tp.getJavaAccess(false);
0N/A String ja_64 = tp.getJavaAccess(true);
0N/A String ja = ja_32;
0N/A int elemSize_32 = AtomicType.getNativeSizeForAccess(ja_32);
0N/A int elemSize_64 = AtomicType.getNativeSizeForAccess(ja_64);
0N/A String elemSize = tp.getItemSize();
0N/A if (type == AtomicType.TYPE_ARRAY) {
0N/A acc_size_32 += elemSize_32 * tp.getArrayLength();
0N/A acc_size_64 += elemSize_64 * tp.getArrayLength();
0N/A pw.println(pref + tp.getJavaType() + " get_" +name + "(int index) { " +s_log+"return " +
0N/A tp.getJavaResult(stp.getOffset(tp) + "+index*" + elemSize, null) + "; }");
0N/A if (tp.getReferencedType() instanceof AtomicType) { // Set for StructType is forbidden
0N/A pw.println(MessageFormat.format(pref + "void set_{0}(int index, {1} v) '{' {3} {2}; '}'",
0N/A new Object[] {
0N/A name, jt,
0N/A tp.getJavaConversion("pData+"+stp.getOffset(tp)+" + index*" + elemSize, "v"),
0N/A s_log}));
0N/A }
0N/A // Returns pointer to the start of the array
0N/A pw.println(pref + "long get_" +name+ "() { "+s_log+"return pData+"+stp.getOffset(tp)+"; }");
0N/A } else if (type == AtomicType.TYPE_PTR) {
0N/A pw.println(MessageFormat.format(pref + "{0} get_{1}(int index) '{' {3} return {2}; '}'",
0N/A new Object[] {
0N/A jt, name,
0N/A tp.getJavaResult("index*" + elemSize, "Native.getLong(pData+"+stp.getOffset(tp)+")"),
0N/A s_log
0N/A }));
0N/A pw.println(pref + "long get_" +name+ "() { "+s_log+"return Native.getLong(pData+"+stp.getOffset(tp)+"); }");
0N/A pw.println(MessageFormat.format(pref + "void set_{0}({1} v) '{' {3} {2}; '}'",
0N/A new Object[] {name, "long", "Native.putLong(pData + " + stp.getOffset(tp) + ", v)", s_log}));
0N/A acc_size_32 += elemSize_32;
0N/A acc_size_64 += elemSize_64;
0N/A } else {
0N/A acc_size_32 += elemSize_32;
0N/A acc_size_64 += elemSize_64;
0N/A pw.println(pref + tp.getJavaType() + " get_" +name +
0N/A "() { "+s_log+"return " + tp.getJavaResult(stp.getOffset(tp), null) + "; }");
0N/A if (type != AtomicType.TYPE_STRUCT) {
0N/A pw.println(MessageFormat.format(pref + "void set_{0}({1} v) '{' {3} {2}; '}'",
0N/A new Object[] {name, jt, tp.getJavaConversion("pData+"+stp.getOffset(tp), "v"), s_log}));
0N/A }
0N/A }
0N/A i++;
0N/A }
0N/A }
0N/A if (s_size_32 != null && !s_size_32.equals(Integer.toString(acc_size_32))) {
0N/A log.fine("32 bits: The size of the structure " + stp.getName() + " " + s_size_32 +
0N/A " is not equal to the accumulated size " +acc_size_32 + " of the fields");
0N/A } else if (s_size_64 != null && !s_size_64.equals(Integer.toString(acc_size_64))) {
0N/A log.fine("64 bits: The size of the structure " + stp.getName() + " " +s_size_64+
0N/A " is not equal to the accumulated size " +acc_size_64+" of the fields");
0N/A }
0N/A }
0N/A
0N/A public void writeWrapperSubclass(StructType stp, PrintWriter pw, boolean wide) {
0N/A
0N/A
0N/A pw.println("class " + stp.getJavaClassName() + "AccessorImpl" + " extends " + stp.getJavaClassName() + "Accessor {");
0N/A pw.println("/*\nThis class serves as a Wrapper for the following X Struct \nsThe offsets here are calculated based on actual compiler.\n\n" +stp.getDescription() + "\n\n */");
0N/A
0N/A writeAccessorImpls(stp, pw);
0N/A
0N/A pw.println("\n\n } \n\n");
0N/A }
0N/A
0N/A public void writeWrapper(String outputDir, StructType stp)
0N/A {
0N/A if (stp.getNumFields() > 0) {
0N/A
0N/A try {
0N/A FileOutputStream fs = new FileOutputStream(outputDir + "/"+stp.getJavaClassName()+".java");
0N/A PrintWriter pw = new PrintWriter(fs);
0N/A pw.println("// This file is an automatically generated file, please do not edit this file, modify the WrapperGenerator.java file instead !\n" );
0N/A
0N/A pw.println("package "+package_name+";\n");
0N/A pw.println("import sun.misc.*;\n");
1696N/A pw.println("import sun.util.logging.PlatformLogger;");
0N/A String baseClass = stp.getBaseClass();
0N/A if (baseClass == null) {
0N/A baseClass = defaultBaseClass;
0N/A }
0N/A if (stp.getIsInterface()) {
0N/A pw.print("public interface ");
0N/A pw.print(stp.getJavaClassName());
0N/A } else {
0N/A pw.print("public class ");
0N/A pw.print(stp.getJavaClassName() + " extends " + baseClass);
0N/A }
0N/A if (stp.getInterfaces() != null) {
0N/A pw.print(" implements " + stp.getInterfaces());
0N/A }
0N/A pw.println(" { ");
0N/A if (!stp.getIsInterface()) {
0N/A pw.println("\tprivate Unsafe unsafe = XlibWrapper.unsafe; ");
0N/A pw.println("\tprivate final boolean should_free_memory;");
0N/A pw.println("\tpublic static int getSize() { return " + stp.getSize() + "; }");
0N/A pw.println("\tpublic int getDataSize() { return getSize(); }");
0N/A pw.println("\n\tlong pData;");
0N/A pw.println("\n\tpublic long getPData() { return pData; }");
0N/A
1045N/A pw.println("\n\n\tpublic " + stp.getJavaClassName() + "(long addr) {");
0N/A if (generateLog) {
0N/A pw.println("\t\tlog.finest(\"Creating\");");
0N/A }
0N/A pw.println("\t\tpData=addr;");
0N/A pw.println("\t\tshould_free_memory = false;");
0N/A pw.println("\t}");
1045N/A pw.println("\n\n\tpublic " + stp.getJavaClassName() + "() {");
0N/A if (generateLog) {
0N/A pw.println("\t\tlog.finest(\"Creating\");");
0N/A }
0N/A pw.println("\t\tpData = unsafe.allocateMemory(getSize());");
0N/A pw.println("\t\tshould_free_memory = true;");
0N/A pw.println("\t}");
0N/A
0N/A pw.println("\n\n\tpublic void dispose() {");
0N/A if (generateLog) {
0N/A pw.println("\t\tlog.finest(\"Disposing\");");
0N/A }
0N/A pw.println("\t\tif (should_free_memory) {");
0N/A if (generateLog) {
0N/A pw.println("\t\t\tlog.finest(\"freeing memory\");");
0N/A }
0N/A pw.println("\t\t\tunsafe.freeMemory(pData); \n\t}");
0N/A pw.println("\t\t}");
0N/A writeAccessorImpls(stp, pw);
0N/A writeToString(stp,pw);
0N/A } else {
0N/A pw.println("\n\n\tvoid dispose();");
0N/A pw.println("\n\tlong getPData();");
0N/A writeStubs(stp,pw);
0N/A }
0N/A
0N/A
0N/A pw.println("}\n\n\n");
0N/A pw.close();
0N/A }
0N/A catch (Exception e)
0N/A {
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A }
0N/A
0N/A private boolean readSizeInfo(InputStream is, boolean wide) {
0N/A String line;
0N/A String splits[];
0N/A BufferedReader in = new BufferedReader(new InputStreamReader(is));
0N/A try {
0N/A while ((line = in.readLine()) != null)
0N/A {
0N/A splits = line.split("\\p{Space}");
0N/A if (splits.length == 2)
0N/A {
0N/A if (wide) {
0N/A sizeTable64bit.put(splits[0],splits[1]);
0N/A } else {
0N/A sizeTable32bit.put(splits[0],splits[1]);
0N/A }
0N/A }
0N/A }
0N/A return true;
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A public void writeFunctionCallWrapper(String outputDir, FunctionType ft) {
0N/A try {
0N/A FileOutputStream fs = new FileOutputStream(outputDir + "/" + ft.getName()+".java");
0N/A PrintWriter pw = new PrintWriter(fs);
0N/A pw.println("// This file is an automatically generated file, please do not edit this file, modify the WrapperGenerator.java file instead !\n" );
0N/A
0N/A pw.println("package "+package_name+";\n");
0N/A pw.println("import sun.misc.Unsafe;\n");
0N/A pw.println("class " + ft.getName() + " {");
0N/A pw.println("\tprivate static Unsafe unsafe = XlibWrapper.unsafe;");
0N/A pw.println("\tprivate boolean __executed = false;");
0N/A pw.println("\tprivate boolean __disposed = false;");
0N/A Iterator iter = ft.getArguments().iterator();
0N/A while (iter.hasNext()) {
0N/A AtomicType at = (AtomicType)iter.next();
0N/A if (at.isIn()) {
0N/A pw.println("\t" + at.getJavaType() + " _" + at.getName() + ";");
0N/A } else {
0N/A pw.println("\tlong " + at.getName() + "_ptr = unsafe.allocateMemory(Native.get" + at.getTypeUpperCase() + "Size());");
0N/A }
0N/A }
0N/A pw.println("\tpublic " + ft.getName() + "(");
0N/A iter = ft.getArguments().iterator();
0N/A boolean first = true;
0N/A while (iter.hasNext()) {
0N/A AtomicType at = (AtomicType)iter.next();
0N/A if (at.isIn() || at.isInOut()) {
0N/A if (!first) {
0N/A pw.println(",");
0N/A }
0N/A first = false;
0N/A pw.print("\t\t" + at.getJavaType() + " " + at.getName());
0N/A }
0N/A }
0N/A pw.println("\t)");
0N/A pw.println("\t{");
0N/A iter = ft.getArguments().iterator();
0N/A while (iter.hasNext()) {
0N/A AtomicType at = (AtomicType)iter.next();
0N/A if (at.isIn() || at.isInOut()) {
0N/A pw.println("\t\tset_" + at.getName() + "(" + at.getName() + ");");
0N/A }
0N/A }
0N/A pw.println("\t}");
0N/A
0N/A pw.println("\tpublic " + ft.getReturnType() + " execute() {");
0N/A if (ft.isVoid()) {
0N/A pw.println("\t\texecute(null);");
0N/A } else {
0N/A pw.println("\t\treturn execute(null);");
0N/A }
0N/A pw.println("\t}");
0N/A
0N/A pw.println("\tpublic " + ft.getReturnType() + " execute(XToolkit.XErrorHandler errorHandler) {");
0N/A pw.println("\t\tif (__disposed) {");
0N/A pw.println("\t\t throw new IllegalStateException(\"Disposed\");");
0N/A pw.println("\t\t}");
0N/A pw.println("\t\tXToolkit.awtLock();");
0N/A pw.println("\t\ttry {");
0N/A pw.println("\t\t\tif (__executed) {");
0N/A pw.println("\t\t\t throw new IllegalStateException(\"Already executed\");");
0N/A pw.println("\t\t\t}");
0N/A pw.println("\t\t\t__executed = true;");
0N/A pw.println("\t\t\tif (errorHandler != null) {");
6447N/A pw.println("\t\t\t XErrorHandlerUtil.WITH_XERROR_HANDLER(errorHandler);");
0N/A pw.println("\t\t\t}");
0N/A iter = ft.getArguments().iterator();
0N/A while (iter.hasNext()) {
0N/A AtomicType at = (AtomicType)iter.next();
0N/A if (!at.isIn() && at.isAutoFree()) {
0N/A pw.println("\t\t\tNative.put" + at.getTypeUpperCase() + "(" +at.getName() + "_ptr, 0);");
0N/A }
0N/A }
0N/A if (!ft.isVoid()) {
0N/A pw.println("\t\t\t" + ft.getReturnType() + " status = ");
0N/A }
0N/A pw.println("\t\t\tXlibWrapper." + ft.getName() + "(XToolkit.getDisplay(), ");
0N/A iter = ft.getArguments().iterator();
0N/A first = true;
0N/A while (iter.hasNext()) {
0N/A AtomicType at = (AtomicType)iter.next();
0N/A if (!first) {
0N/A pw.println(",");
0N/A }
0N/A first = false;
0N/A if (at.isIn()) {
0N/A pw.print("\t\t\t\tget_" + at.getName() + "()");
0N/A } else {
0N/A pw.print("\t\t\t\t" + at.getName() + "_ptr");
0N/A }
0N/A }
0N/A pw.println("\t\t\t);");
0N/A pw.println("\t\t\tif (errorHandler != null) {");
6447N/A pw.println("\t\t\t XErrorHandlerUtil.RESTORE_XERROR_HANDLER();");
0N/A pw.println("\t\t\t}");
0N/A if (!ft.isVoid()) {
0N/A pw.println("\t\t\treturn status;");
0N/A }
0N/A pw.println("\t\t} finally {");
0N/A pw.println("\t\t XToolkit.awtUnlock();");
0N/A pw.println("\t\t}");
0N/A pw.println("\t}");
0N/A
0N/A pw.println("\tpublic boolean isExecuted() {");
0N/A pw.println("\t return __executed;");
0N/A pw.println("\t}");
0N/A pw.println("\t");
0N/A pw.println("\tpublic boolean isDisposed() {");
0N/A pw.println("\t return __disposed;");
0N/A pw.println("\t}");
0N/A pw.println("\tpublic void finalize() {");
0N/A pw.println("\t dispose();");
0N/A pw.println("\t}");
0N/A
0N/A pw.println("\tpublic void dispose() {");
0N/A pw.println("\t\tXToolkit.awtLock();");
0N/A pw.println("\t\ttry {");
0N/A pw.println("\t\tif (__disposed || !__executed) {");
0N/A pw.println("\t\t return;");
0N/A pw.println("\t\t} finally {");
0N/A pw.println("\t\t XToolkit.awtUnlock();");
0N/A pw.println("\t\t}");
0N/A pw.println("\t\t}");
0N/A
0N/A iter = ft.getArguments().iterator();
0N/A while (iter.hasNext()) {
0N/A AtomicType at = (AtomicType)iter.next();
0N/A if (!at.isIn()) {
0N/A if (at.isAutoFree()) {
0N/A pw.println("\t\tif (__executed && get_" + at.getName() + "()!= 0) {");
0N/A pw.println("\t\t\tXlibWrapper.XFree(get_" + at.getName() + "());");
0N/A pw.println("\t\t}");
0N/A }
0N/A pw.println("\t\tunsafe.freeMemory(" + at.getName() + "_ptr);");
0N/A }
0N/A }
0N/A pw.println("\t\t__disposed = true;");
0N/A pw.println("\t\t}");
0N/A pw.println("\t}");
0N/A
0N/A iter = ft.getArguments().iterator();
0N/A while (iter.hasNext()) {
0N/A AtomicType at = (AtomicType)iter.next();
0N/A pw.println("\tpublic " + at.getJavaType() + " get_" + at.getName() + "() {");
0N/A
0N/A pw.println("\t\tif (__disposed) {");
0N/A pw.println("\t\t throw new IllegalStateException(\"Disposed\");");
0N/A pw.println("\t\t}");
0N/A pw.println("\t\tif (!__executed) {");
0N/A pw.println("\t\t throw new IllegalStateException(\"Not executed\");");
0N/A pw.println("\t\t}");
0N/A
0N/A if (at.isIn()) {
0N/A pw.println("\t\treturn _" + at.getName() + ";");
0N/A } else {
0N/A pw.println("\t\treturn Native.get" + at.getTypeUpperCase() + "(" + at.getName() + "_ptr);");
0N/A }
0N/A pw.println("\t}");
0N/A
0N/A pw.println("\tpublic void set_" + at.getName() + "(" + at.getJavaType() + " data) {");
0N/A if (at.isIn()) {
0N/A pw.println("\t\t_" + at.getName() + " = data;");
0N/A } else {
0N/A pw.println("\t\tNative.put" + at.getTypeUpperCase() + "(" + at.getName() + "_ptr, data);");
0N/A }
0N/A pw.println("\t}");
0N/A }
0N/A pw.println("}");
0N/A pw.close();
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A
0N/A public void writeJavaWrapperClass(String outputDir) {
0N/A// (new File(outputDir, package_path)).mkdirs();
0N/A try {
0N/A for (Enumeration e = symbolTable.elements() ; e.hasMoreElements() ;) {
0N/A BaseType tp = (BaseType) e.nextElement();
0N/A if (tp instanceof StructType) {
0N/A StructType st = (StructType) tp;
0N/A writeWrapper(outputDir, st);
0N/A } else if (tp instanceof FunctionType) {
0N/A writeFunctionCallWrapper(outputDir, (FunctionType)tp);
0N/A }
0N/A }
0N/A }
0N/A catch (Exception e) {
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A
0N/A
0N/A public void writeNativeSizer(String file)
0N/A {
0N/A int type;
0N/A int i=0;
0N/A int j=0;
0N/A BaseType tp;
0N/A StructType stp;
0N/A Enumeration eo;
0N/A
0N/A
0N/A try {
0N/A
0N/A FileOutputStream fs = new FileOutputStream(file);
0N/A PrintWriter pw = new PrintWriter(fs);
0N/A
0N/A pw.println("/* This file is an automatically generated file, please do not edit this file, modify the XlibParser.java file instead !*/\n" );
0N/A pw.println("#include <X11/Xlib.h>\n#include <X11/Xutil.h>\n#include <X11/Xos.h>\n#include <X11/Xatom.h>\n#include <stdio.h>\n");
0N/A pw.println("#include <X11/extensions/Xdbe.h>");
1067N/A pw.println("#include <X11/XKBlib.h>");
0N/A pw.println("#include \"awt_p.h\"");
0N/A pw.println("#include \"color.h\"");
0N/A pw.println("#include \"colordata.h\"");
0N/A pw.println("\ntypedef struct\n");
0N/A pw.println("{\n");
0N/A pw.println(" unsigned long flags;\n");
0N/A pw.println(" unsigned long functions;\n");
0N/A pw.println(" unsigned long decorations;\n");
0N/A pw.println(" long inputMode;\n");
0N/A pw.println(" unsigned long status;\n");
0N/A pw.println("} PropMwmHints;\n");
0N/A
0N/A
0N/A pw.println("\n\nint main(){");
0N/A j=0;
0N/A for ( eo = symbolTable.elements() ; eo.hasMoreElements() ;) {
0N/A tp = (BaseType) eo.nextElement();
0N/A if (tp instanceof StructType)
0N/A {
0N/A stp = (StructType) tp;
0N/A if (!stp.getIsInterface()) {
0N/A pw.println(stp.getName()+" temp"+ j + ";\n");
0N/A j++;
0N/A }
0N/A }
0N/A }
0N/A j=0;
0N/A
0N/A pw.println("printf(\"long\t%d\\n\",(int)sizeof(long));");
0N/A pw.println("printf(\"int\t%d\\n\",(int)sizeof(int));");
0N/A pw.println("printf(\"short\t%d\\n\",(int)sizeof(short));");
0N/A pw.println("printf(\"ptr\t%d\\n\",(int)sizeof(void *));");
0N/A pw.println("printf(\"Bool\t%d\\n\",(int)sizeof(Bool));");
0N/A pw.println("printf(\"Atom\t%d\\n\",(int)sizeof(Atom));");
0N/A pw.println("printf(\"Window\t%d\\n\",(int)sizeof(Window));");
0N/A
0N/A
0N/A for (eo = symbolTable.elements() ; eo.hasMoreElements() ;) {
0N/A
0N/A
0N/A tp = (BaseType) eo.nextElement();
0N/A if (tp instanceof StructType)
0N/A {
0N/A stp = (StructType) tp;
0N/A if (stp.getIsInterface()) {
0N/A continue;
0N/A }
0N/A for (Enumeration e = stp.getMembers() ; e.hasMoreElements() ;) {
0N/A AtomicType atp = (AtomicType) e.nextElement();
0N/A if (atp.isAlias()) continue;
0N/A pw.println("printf(\""+ stp.getName() + "." + atp.getName() + "\t%d\\n\""+
0N/A ",(int)((unsigned long ) &temp"+j+"."+atp.getName()+"- (unsigned long ) &temp" + j + ") );");
0N/A
0N/A i++;
0N/A
0N/A
0N/A }
0N/A pw.println("printf(\""+ stp.getName() + "\t%d\\n\"" + ",(int)sizeof(temp"+j+"));");
0N/A
0N/A j++;
0N/A }
0N/A
0N/A }
0N/A pw.println("return 0;");
0N/A pw.println("}");
0N/A pw.close();
0N/A
0N/A }
0N/A catch (Exception e)
0N/A {
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A
0N/A private void initTypes() {
0N/A symbolTable.put("int", new AtomicType(AtomicType.TYPE_INT, "", "int"));
0N/A symbolTable.put("short", new AtomicType(AtomicType.TYPE_SHORT, "", "short"));
0N/A symbolTable.put("long", new AtomicType(AtomicType.TYPE_LONG, "", "long"));
0N/A symbolTable.put("float", new AtomicType(AtomicType.TYPE_FLOAT, "", "float"));
0N/A symbolTable.put("double", new AtomicType(AtomicType.TYPE_DOUBLE, "", "double"));
0N/A symbolTable.put("Bool", new AtomicType(AtomicType.TYPE_BOOL, "", "Bool"));
0N/A symbolTable.put("char", new AtomicType(AtomicType.TYPE_CHAR, "", "char"));
0N/A symbolTable.put("byte", new AtomicType(AtomicType.TYPE_BYTE, "", "byte"));
0N/A symbolTable.put("pointer", new AtomicType(AtomicType.TYPE_PTR, "", "pointer"));
0N/A symbolTable.put("longlong", new AtomicType(AtomicType.TYPE_LONG_LONG, "", "longlong"));
0N/A symbolTable.put("Atom", new AtomicType(AtomicType.TYPE_ATOM, "", "Atom"));
0N/A symbolTable.put("ulong", new AtomicType(AtomicType.TYPE_ULONG, "", "ulong"));
0N/A }
0N/A public WrapperGenerator(String outputDir, String xlibFilename) {
0N/A initTypes();
0N/A try {
0N/A BufferedReader in = new BufferedReader(new FileReader(xlibFilename));
0N/A String line;
0N/A String splits[];
0N/A BaseType curType = null;
0N/A while ((line = in.readLine()) != null)
0N/A {
0N/A int commentStart = line.indexOf("//");
0N/A if (commentStart >= 0) {
0N/A // remove comment
0N/A line = line.substring(0, commentStart);
0N/A }
0N/A
0N/A if ("".equals(line)) {
0N/A // skip empty line
0N/A continue;
0N/A }
0N/A
0N/A splits = line.split("\\p{Space}+");
0N/A if (splits.length >= 2)
0N/A {
0N/A String struct_name = curType.getName();
0N/A String field_name = splits[1];
0N/A String s_type = splits[2];
0N/A BaseType bt = curType;
0N/A int type = AtomicType.getTypeForString(s_type);
0N/A AtomicType atp = null;
0N/A if (bt != null && type != -1) {
0N/A atp = new AtomicType(type,field_name,s_type);
0N/A if (splits.length > 3) {
0N/A atp.setAttributes(splits);
0N/A }
0N/A if (bt instanceof StructType) {
0N/A StructType stp = (StructType) bt;
0N/A stp.addMember(atp);
0N/A } else if (bt instanceof FunctionType) {
0N/A ((FunctionType)bt).addArgument(atp);
0N/A }
0N/A }
0N/A else if (bt == null) {
0N/A System.out.println("Cannot find " + struct_name);
0N/A }
0N/A
0N/A }
0N/A else if (line != null) {
0N/A BaseType bt = (BaseType) symbolTable.get(line);
0N/A if (bt == null) {
0N/A if (line.startsWith("!")) {
0N/A FunctionType ft = new FunctionType(line);
0N/A ft.setName(line);
0N/A symbolTable.put(ft.getName(),ft);
0N/A curType = ft;
0N/A } else {
0N/A StructType stp = new StructType(line);
0N/A stp.setName(line);
0N/A curType = stp;
0N/A symbolTable.put(stp.getName(),stp);
0N/A }
0N/A }
0N/A }
0N/A
0N/A }
0N/A in.close();
0N/A }
0N/A catch (Exception e) {
0N/A e.printStackTrace();
0N/A }
0N/A
0N/A }
0N/A private void makeSizer(String outputDir) {
0N/A if (wide) {
0N/A sizerFileName = "sizer.64.c";
0N/A } else {
0N/A sizerFileName = "sizer.32.c";
0N/A }
0N/A File fp = new File(outputDir, sizerFileName);
0N/A writeNativeSizer(fp.getAbsolutePath());
0N/A }
0N/A private boolean readSizeInfo(String sizeInfo) {
0N/A try {
0N/A File f = new File(sizeInfo+".32");
0N/A boolean res = true;
0N/A FileInputStream fis = null;
0N/A if (f.exists()) {
0N/A fis = new FileInputStream(f);
0N/A res = readSizeInfo(fis, false);
0N/A fis.close();
0N/A }
0N/A f = new File(sizeInfo+".64");
0N/A if (f.exists()) {
0N/A fis = new FileInputStream(f);
0N/A res &= readSizeInfo(fis, true);
0N/A fis.close();
0N/A }
0N/A return res;
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A private void startGeneration(String outputDir, String sizeInfo) {
0N/A if (readSizeInfo(sizeInfo))
0N/A {
0N/A writeJavaWrapperClass(outputDir);
0N/A }
0N/A else {
0N/A System.out.println("Error calculating offsets");
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) {
0N/A
0N/A if (args.length < 4) {
0N/A System.out.println("Usage:\nWrapperGenerator <output_dir> <xlibtypes.txt> <action> [<platform> | <sizes info file>]");
0N/A System.out.println("Where <action>: gen, sizer");
0N/A System.out.println(" <platform>: 32, 64");
0N/A System.exit(1);
0N/A }
0N/A
0N/A WrapperGenerator xparser = new WrapperGenerator(args[0], args[1]);
0N/A if (args[2].equals("sizer")) {
0N/A xparser.wide = args[3].equals("64");
0N/A xparser.makeSizer(args[0]);
0N/A } else if (args[2].equals("gen")) {
0N/A xparser.startGeneration(args[0], args[3]);
0N/A }
0N/A }
0N/A
0N/A}