1 /*
   2  * Copyright (c) 2003, 2009, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 import java.util.*;
  27 import java.io.*;
  28 import java.nio.charset.*;
  29 import java.text.MessageFormat;
  30 import java.util.logging.*;
  31 
  32 public class WrapperGenerator {
  33     /* XLibParser converts Xlib.h to a Java Object that encapsulates the
  34      * X11 API and data structures */
  35     // Charset and decoder for ISO-8859-15
  36     private final static Logger log = Logger.getLogger("WrapperGenerator");
  37     boolean generateLog = true;
  38     boolean wide;
  39     private static Charset charset = Charset.forName("ISO-8859-15");
  40 
  41     String package_name = "sun.awt.X11";
  42     String package_path = "sun/awt/X11";
  43     String sizerFileName = "sizer.c";
  44     String defaultBaseClass = "XWrapperBase";
  45 
  46     String compile_options = "-lX11";
  47     static Hashtable symbolTable = new Hashtable();
  48     static Hashtable sizeTable32bit = new Hashtable();
  49     static Hashtable sizeTable64bit = new Hashtable();
  50     static Hashtable knownSizes32 = new Hashtable();
  51     static Hashtable knownSizes64 = new Hashtable();
  52     static {
  53 /*
  54         knownSizes64.put("", Integer.valueOf());
  55         knownSizes32.put("", Integer.valueOf());
  56 */
  57         knownSizes64.put("XComposeStatus", Integer.valueOf(16));
  58         knownSizes64.put("XTimeCoord", Integer.valueOf(16));
  59         knownSizes64.put("XExtData", Integer.valueOf(32));
  60         knownSizes64.put("XWindowChanges", Integer.valueOf(40));
  61         knownSizes64.put("XOMCharSetList", Integer.valueOf(16));
  62         knownSizes64.put("XModifierKeymap", Integer.valueOf(16));
  63         knownSizes32.put("XIMValuesList", Integer.valueOf(8));
  64         knownSizes32.put("XGCValues", Integer.valueOf(92));
  65 //          knownSizes32.put("XIMStringConversionCallbackStruct", Integer.valueOf(16));
  66     }
  67 
  68     private static abstract class BaseType {
  69 
  70         String real_type;
  71         String name;
  72 
  73 
  74         public String getName() {
  75             return name;
  76         }
  77         public String getRealType() {
  78             return real_type;
  79         }
  80 
  81         public String toString() {
  82             return name;
  83         }
  84     }
  85 
  86     private static class AtomicType extends BaseType {
  87 
  88         private boolean alias;
  89         private String aliasName;
  90 
  91         static final int TYPE_INT=0;
  92         static final int TYPE_CHAR=1;
  93         static final int TYPE_LONG=2;
  94         static final int TYPE_LONG_LONG=3;
  95         static final int TYPE_DOUBLE=4;
  96         static final int TYPE_FLOAT=5;
  97         static final int TYPE_PTR=6;
  98         static final int TYPE_SHORT=7;
  99         static final int TYPE_BOOL = 8;
 100         static final int TYPE_STRUCT = 9;
 101         static final int TYPE_ARRAY = 10;
 102         static final int TYPE_BYTE=11;
 103         static final int TYPE_ATOM = 12;
 104         static final int TYPE_ULONG = 13;
 105         static int getTypeForString(String str) {
 106             int type=-1;
 107             if (str.equals("int"))
 108                 type = AtomicType.TYPE_INT;
 109             else if (str.equals("long"))
 110                 type = AtomicType.TYPE_LONG;
 111             else if (str.equals("byte"))
 112                 type = AtomicType.TYPE_BYTE;
 113             else if (str.equals("char"))
 114                 type = AtomicType.TYPE_CHAR;
 115             else if (str.equals("long long"))
 116                 type = AtomicType.TYPE_LONG_LONG;
 117             else if (str.equals("double"))
 118                 type = AtomicType.TYPE_DOUBLE;
 119             else if (str.equals("float"))
 120                 type = AtomicType.TYPE_FLOAT;
 121             else if (str.equals("pointer"))
 122                 type = AtomicType.TYPE_PTR;
 123             else if (str.equals("short"))
 124                 type = AtomicType.TYPE_SHORT;
 125             else if (str.equals("Bool"))
 126                 type = AtomicType.TYPE_BOOL;
 127             else if (str.equals("struct"))
 128                 type = AtomicType.TYPE_STRUCT;
 129             else if (str.equals("Atom"))
 130                 type = AtomicType.TYPE_ATOM;
 131             else if (str.equals("array"))
 132                 type = TYPE_ARRAY;
 133             else if (str.equals("ulong"))
 134                 type = TYPE_ULONG;
 135             else throw new IllegalArgumentException("Uknown type string: " + str);
 136 
 137             return type;
 138         }
 139         String getJavaType() {
 140             if (referencedType != null) {
 141                 if (referencedType instanceof AtomicType) {
 142                     return ((AtomicType)referencedType).getJavaType();
 143                 } else {
 144                     return referencedType.getName();
 145                 }
 146             } else {
 147                 return getJavaTypeForType(type);
 148             }
 149         }
 150         static String getJavaTypeForType(int type) {
 151             switch (type) {
 152               case TYPE_INT:
 153                   return "int";
 154               case TYPE_CHAR:
 155                   return "char";
 156               case TYPE_BYTE:
 157                   return "byte";
 158               case TYPE_LONG:
 159               case TYPE_LONG_LONG:
 160               case TYPE_PTR:
 161               case TYPE_ULONG:
 162                   return "long";
 163               case TYPE_DOUBLE:
 164                   return "double";
 165               case TYPE_FLOAT:
 166                   return "float";
 167               case TYPE_SHORT:
 168                   return "short";
 169               case TYPE_BOOL:
 170                   return "boolean";
 171               case TYPE_ATOM:
 172                   return "long";
 173               default:
 174                   throw new IllegalArgumentException("Unknown type: " + type);
 175             }
 176         }
 177         String getItemSize() {
 178             if (referencedType != null) {
 179                   if (referencedType instanceof StructType) {
 180                       return ((StructType)referencedType).getSize();
 181                   } else {
 182                       return ((AtomicType)referencedType).getItemSize();
 183                   }
 184             } else {
 185                 int i32 = getNativeSizeForAccess(getJavaAccess(false));
 186                 int i64 = getNativeSizeForAccess(getJavaAccess(true));
 187                 if (i32 != i64) {
 188                     return "Native.get" + getNativeAccess() + "Size()";
 189                 } else {
 190                     return Integer.toString(i32);
 191                 }
 192             }
 193         }
 194 
 195         String getJavaResult(String offset, String base) {
 196             String res = null;
 197             switch (type) {
 198               case TYPE_STRUCT:
 199                   res = "pData + " + offset;
 200                   break;
 201               case TYPE_PTR:
 202                   if (referencedType == null || referencedType instanceof StructType) {
 203                       res = base + "+" + offset;
 204                   } else if (referencedType instanceof AtomicType) {
 205                       res = MessageFormat.format("Native.get{0}({1})",
 206                                                  new Object[] {getNativeAccessForType(((AtomicType)referencedType).type),
 207                                                                base + "+" + offset});
 208                   }
 209                   break;
 210               case TYPE_ARRAY:
 211                   if (referencedType instanceof StructType) {
 212                       res = "pData + " + offset;
 213                   }  else if (referencedType instanceof AtomicType) {
 214                       res = MessageFormat.format("Native.get{0}(pData + {1})",
 215                                                  new Object[] {getNativeAccessForType(((AtomicType)referencedType).type),
 216                                                                offset});
 217                   }
 218                   break;
 219               default:
 220                 res = MessageFormat.format("(Native.get{0}(pData+{1}))",
 221                                            new Object[] {getNativeAccess(), offset});
 222             }
 223             return getJavaResultConversion(res, base);
 224         }
 225         String getJavaResultConversion(String value, String base) {
 226             if (referencedType != null) {
 227                 if (referencedType instanceof StructType) {
 228                     if (type == TYPE_PTR) {
 229                         return MessageFormat.format("({2} != 0)?(new {0}({1})):(null)", new Object[] {referencedType.getName(),value, base});
 230                     } else {
 231                         return MessageFormat.format("new {0}({1})", new Object[] {referencedType.getName(),value});
 232                     }
 233                 } else {
 234                     return value;
 235                 }
 236             } else {
 237                 return getJavaResultConversionForType(type, value);
 238             }
 239         }
 240         static String getJavaResultConversionForType(int type, String value) {
 241             return value;
 242         }
 243         String getNativeAccess() {
 244             return getNativeAccessForType(type);
 245         }
 246         String getJavaAccess(boolean wide) {
 247             return getJavaAccessForType(type, wide);
 248         }
 249         static String getJavaAccessForType(int type, boolean wide) {
 250             switch (type) {
 251               case TYPE_INT:
 252                   return "Int";
 253               case TYPE_CHAR:
 254                   return "Char";
 255               case TYPE_BYTE:
 256                   return "Byte";
 257               case TYPE_LONG:
 258               case TYPE_PTR:
 259               case TYPE_ARRAY:
 260               case TYPE_STRUCT:
 261               case TYPE_ATOM:
 262                   return (wide?"Long":"Int");
 263               case TYPE_LONG_LONG:
 264                   return "Long";
 265               case TYPE_ULONG:
 266                   return (wide?"ULong":"UInt");
 267               case TYPE_DOUBLE:
 268                   return "Double";
 269               case TYPE_FLOAT:
 270                   return "Float";
 271               case TYPE_SHORT:
 272                   return "Short";
 273               case TYPE_BOOL:
 274                   return "Int";
 275               default:
 276                   throw new IllegalArgumentException("Unknown type: " + type);
 277             }
 278         }
 279         static String getNativeAccessForType(int type) {
 280             switch (type) {
 281               case TYPE_INT:
 282                   return "Int";
 283               case TYPE_CHAR:
 284                   return "Char";
 285               case TYPE_BYTE:
 286                   return "Byte";
 287               case TYPE_LONG:
 288               case TYPE_PTR:
 289               case TYPE_ARRAY:
 290               case TYPE_STRUCT:
 291                   return "Long";
 292               case TYPE_LONG_LONG:
 293                   return "Long";
 294               case TYPE_ULONG:
 295                   return "ULong";
 296               case TYPE_DOUBLE:
 297                   return "Double";
 298               case TYPE_FLOAT:
 299                   return "Float";
 300               case TYPE_SHORT:
 301                   return "Short";
 302               case TYPE_BOOL:
 303                   return "Bool";
 304               case TYPE_ATOM:
 305                   return "Long";
 306               default:
 307                   throw new IllegalArgumentException("Unknown type: " + type);
 308             }
 309         }
 310 
 311         static int getNativeSizeForAccess(String access) {
 312             if (access.equals("Int")) return 4;
 313             else if (access.equals("Byte")) return 1;
 314             else if (access.equals("Long")) return 8;
 315             else if (access.equals("Double")) return 8;
 316             else if (access.equals("Float")) return 4;
 317             else if (access.equals("Char")) return 2;
 318             else if (access.equals("Short")) return 2;
 319             else if (access.equals("ULong")) return 8;
 320             else if (access.equals("UInt")) return 4;
 321             else throw new IllegalArgumentException("Unknow access type: " + access);
 322         }
 323 
 324         String getJavaConversion(String offset, String value) {
 325             if (referencedType != null) {
 326                 if (referencedType instanceof StructType) {
 327                     return getJavaConversionForType(TYPE_PTR, offset, value + ".pData");
 328                 } else {
 329                     if (type == TYPE_ARRAY) {
 330                         return getJavaConversionForType(((AtomicType)referencedType).type, offset, value);
 331                     } else { // TYPE_PTR
 332                         return getJavaConversionForType(TYPE_PTR, offset, value);
 333                     }
 334                 }
 335             } else {
 336                 return getJavaConversionForType(type, offset, value);
 337             }
 338         }
 339         static String getJavaConversionForType(int type, String offset, String value) {
 340             return MessageFormat.format("Native.put{0}({2}, {1})", new Object[] {getNativeAccessForType(type), value, offset});
 341         }
 342 
 343 
 344         int type;
 345         int offset;
 346         int direction;
 347         BaseType referencedType;
 348         int arrayLength = -1;
 349         boolean autoFree = false;
 350         public AtomicType(int _type,String _name, String _real_type) {
 351             name = _name.replaceAll("[* \t]","");
 352             if ((name.indexOf("[") != -1) || (name.indexOf("]") != -1))
 353             {
 354                 name = name.replaceAll("\\[.*\\]","");
 355             }
 356             type = _type;
 357             real_type = _real_type;
 358             if (real_type == null)
 359             {
 360                 System.out.println(" real type is null");
 361 
 362             }
 363         }
 364         public boolean isIn() {
 365             return direction == 0;
 366         }
 367         public boolean isOut() {
 368             return direction == 1;
 369         }
 370         public boolean isInOut() {
 371             return direction == 2;
 372         }
 373         public boolean isAutoFree() {
 374             return autoFree;
 375         }
 376         public void setAttributes(String[] attributes) {
 377             String mod = attributes[3];
 378             if ("in".equals(mod)) {
 379                 direction = 0;
 380             } else if ("out".equals(mod)) {
 381                 direction = 1;
 382                 if (attributes.length > 4 && "free".equals(attributes[4])) {
 383                     autoFree = true;
 384                 }
 385             } else if ("inout".equals(mod)) {
 386                 direction = 2;
 387             } else if ("alias".equals(mod)) {
 388                 alias = true;
 389                 aliasName = attributes[4];
 390             } else if (type == TYPE_ARRAY || type == TYPE_PTR || type == TYPE_STRUCT) {
 391                 referencedType = (BaseType)symbolTable.get(mod);
 392                 if (referencedType == null) {
 393                     log.warning("Can't find type for name " + mod);
 394                 }
 395                 if (attributes.length > 4) { // array length
 396                     try {
 397                         arrayLength = Integer.parseInt(attributes[4]);
 398                     } catch (Exception e) {
 399                     }
 400                 }
 401             }
 402         }
 403         public BaseType getReferencedType() {
 404             return referencedType;
 405         }
 406         public int getArrayLength() {
 407             return arrayLength;
 408         }
 409         public void setOffset(int o)
 410         {
 411             offset = o;
 412         }
 413         public int getType() {
 414             return type;
 415         }
 416         public String getTypeUpperCase() {
 417             switch (type) {
 418               case TYPE_INT:
 419                   return "Int";
 420               case TYPE_CHAR:
 421                   return "Char";
 422               case TYPE_BYTE:
 423                   return "Byte";
 424               case TYPE_LONG:
 425               case TYPE_LONG_LONG:
 426               case TYPE_PTR:
 427                   return "Long";
 428               case TYPE_DOUBLE:
 429                   return "Double";
 430               case TYPE_FLOAT:
 431                   return "Float";
 432               case TYPE_SHORT:
 433                   return "Short";
 434               case TYPE_BOOL:
 435                   return "Int";
 436               case TYPE_ATOM:
 437                   return "Long";
 438               case TYPE_ULONG:
 439                   return "ULong";
 440               default: throw new IllegalArgumentException("Uknown type");
 441             }
 442         }
 443         public int getOffset()
 444         {
 445             return offset;
 446         }
 447         public boolean isAlias() {
 448             return alias;
 449         }
 450         public String getAliasName() {
 451             return aliasName;
 452         }
 453     }
 454 
 455     private static class StructType extends BaseType {
 456 
 457         Vector members;
 458         String description;
 459         boolean packed;
 460         int size;
 461         String baseClass, interfaces;
 462         boolean isInterface;
 463         String javaClassName;
 464 
 465         /**
 466          * Construct new structured type.
 467          * Description is used for name and type definition and has the following format:
 468          * structName [ '[' base classe ']' ] [ '{' interfaces '}' ] [ '|' javaClassName ]
 469          */
 470         public StructType(String _desc)
 471         {
 472             members = new Vector();
 473             parseDescription(_desc);
 474         }
 475         public int getNumFields()
 476         {
 477             return members.size();
 478         }
 479         public void setName(String _name)
 480         {
 481             _name = _name.replaceAll("[* \t]","");
 482             parseDescription(_name);
 483         }
 484 
 485         public void setSize(int i)
 486         {
 487             size = i;
 488         }
 489 
 490         public String getDescription()
 491         {
 492             return description;
 493         }
 494 
 495         public Enumeration getMembers()
 496         {
 497             return members.elements();
 498         }
 499 
 500         public void addMember(BaseType tp)
 501         {
 502             members.add(tp);
 503         }
 504         public String getBaseClass() {
 505             return baseClass;
 506         }
 507         public String getInterfaces() {
 508             return interfaces;
 509         }
 510         public boolean getIsInterface() {
 511             return isInterface;
 512         }
 513         public String getJavaClassName() {
 514             return javaClassName;
 515         }
 516         void parseDescription(String _desc) {
 517             if (_desc.indexOf('[') != -1) { // Has base class
 518                 baseClass = _desc.substring(_desc.indexOf('[')+1, _desc.indexOf(']'));
 519                 _desc = _desc.substring(0, _desc.indexOf('[')) + _desc.substring(_desc.indexOf(']')+1);
 520             }
 521             if (_desc.indexOf('{') != -1) { // Has base class
 522                 interfaces = _desc.substring(_desc.indexOf('{')+1, _desc.indexOf('}'));
 523                 _desc = _desc.substring(0, _desc.indexOf('{')) + _desc.substring(_desc.indexOf('}')+1);
 524             }
 525             if (_desc.startsWith("-")) { // Interface
 526                 isInterface = true;
 527                 _desc = _desc.substring(1, _desc.length());
 528             }
 529             if (_desc.indexOf("|") != -1) {
 530                 javaClassName = _desc.substring(_desc.indexOf('|')+1, _desc.length());
 531                 _desc = _desc.substring(0, _desc.indexOf('|'));
 532             }
 533             name = _desc;
 534             if (javaClassName == null) {
 535                 javaClassName = name;
 536             }
 537             description = _desc;
 538 //              System.out.println("Struct " + name + " extends " + baseClass + " implements " + interfaces);
 539         }
 540 
 541         /**
 542          * Returns String containing Java code calculating size of the structure depending on the data model
 543          */
 544         public String getSize() {
 545             String s32 = (String) WrapperGenerator.sizeTable32bit.get(getName());
 546             String s64 = (String) WrapperGenerator.sizeTable64bit.get(getName());
 547             if (s32 == null || s64 == null) {
 548                 return (s32 == null)?(s64):(s32);
 549             }
 550             if (s32.equals(s64)) {
 551                 return s32;
 552             } else {
 553                 return MessageFormat.format("((XlibWrapper.dataModel == 32)?({0}):({1}))", new Object[] {s32, s64});
 554             }
 555         }
 556         public String getOffset(AtomicType atp) {
 557             String key = getName()+"."+(atp.isAlias() ? atp.getAliasName() : atp.getName());
 558             String s64 = (String) WrapperGenerator.sizeTable64bit.get(key);
 559             String s32 = (String) WrapperGenerator.sizeTable32bit.get(key);
 560             if (s32 == null || s64 == null) {
 561                 return (s32 == null)?(s64):(s32);
 562             }
 563             if (s32.equals(s64)) {
 564                 return s32;
 565             } else {
 566                 return MessageFormat.format("((XlibWrapper.dataModel == 32)?({0}):({1}))", new Object[]{s32, s64});
 567             }
 568         }
 569     }
 570 
 571     private static class FunctionType extends BaseType {
 572 
 573         Vector args;
 574         String description;
 575         boolean packed;
 576         String returnType;
 577 
 578         int alignment;
 579 
 580         public FunctionType(String _desc)
 581         {
 582             args = new Vector();
 583             description = _desc;
 584             setName(_desc);
 585         }
 586         boolean isVoid() {
 587             return (returnType == null);
 588         }
 589         String getReturnType() {
 590             if (returnType == null) {
 591                 return "void";
 592             } else {
 593                 return returnType;
 594             }
 595         }
 596 
 597         public int getNumArgs()
 598         {
 599             return args.size();
 600         }
 601         public void setName(String _name)
 602         {
 603             if (_name.startsWith("!")) {
 604                 _name = _name.substring(1, _name.length());
 605             }
 606             if (_name.indexOf("|") != -1) {
 607                 returnType = _name.substring(_name.indexOf("|")+1, _name.length());
 608                 _name = _name.substring(0, _name.indexOf("|"));
 609             }
 610             name = _name.replaceAll("[* \t]","");
 611         }
 612 
 613         public String getDescription()
 614         {
 615             return description;
 616         }
 617 
 618         public Collection getArguments()
 619         {
 620             return args;
 621         }
 622         public void addArgument(BaseType tp)
 623         {
 624             args.add(tp);
 625         }
 626     }
 627 
 628     public String makeComment(String str)
 629     {
 630         StringTokenizer st = new StringTokenizer(str,"\r\n");
 631         String ret="";
 632 
 633         while (st.hasMoreTokens())
 634         {
 635             ret = ret + "//" + st.nextToken() + "\n";
 636         }
 637 
 638         return ret;
 639     }
 640 
 641     public String getJavaTypeForSize(int size) {
 642         switch(size) {
 643           case 1: return "byte";
 644           case 2: return "short";
 645           case 4: return "int";
 646           case 8: return "long";
 647           default: throw new RuntimeException("Unsupported size: " + size);
 648         }
 649     }
 650     public String getOffsets(StructType stp,AtomicType atp, boolean wide)
 651     {
 652         String key = stp.getName()+"."+atp.getName();
 653         return wide == true ? (String) sizeTable64bit.get(key) : (String) sizeTable32bit.get(key);
 654     }
 655 
 656     public String getStructSize(StructType stp, boolean wide)
 657     {
 658         return wide == true ? (String) sizeTable64bit.get(stp.getName()) : (String) sizeTable32bit.get(stp.getName());
 659     }
 660 
 661     public int getLongSize(boolean wide)
 662     {
 663         return Integer.parseInt(wide == true ? (String)sizeTable64bit.get("long") : (String)sizeTable32bit.get("long"));
 664     }
 665 
 666     public int getPtrSize(boolean wide)
 667     {
 668         return Integer.parseInt(wide == true ? (String)sizeTable64bit.get("ptr") : (String)sizeTable32bit.get("ptr"));
 669     }
 670     public int getBoolSize(boolean wide) {
 671         return getOrdinalSize("Bool", wide);
 672     }
 673     public int getOrdinalSize(String ordinal, boolean wide) {
 674         return Integer.parseInt(wide == true ? (String)sizeTable64bit.get(ordinal) : (String)sizeTable32bit.get(ordinal));
 675     }
 676 
 677     public void writeToString(StructType stp, PrintWriter pw) {
 678         int type;
 679         pw.println("\n\n\tString getName() {\n\t\treturn \"" + stp.getName()+ "\"; \n\t}");
 680         pw.println("\n\n\tString getFieldsAsString() {\n\t\tString ret=\"\";\n");
 681 
 682         for (Enumeration e = stp.getMembers() ; e.hasMoreElements() ;) {
 683             AtomicType tp = (AtomicType) e.nextElement();
 684 
 685             type = tp.getType();
 686             String name = tp.getName().replace('.', '_');
 687             if ((name != null) && (name.length() > 0))
 688             {
 689                 if (type == AtomicType.TYPE_ATOM) {
 690                     pw.println("\t\tret += \"\"+\"" + name + " = \" + XAtom.get(get_" + name + "()) +\", \";");
 691                 } else if (name.equals("type")) {
 692                     pw.println("\t\tret += \"\"+\"type = \" + XlibWrapper.eventToString[get_type()] +\", \";");
 693                 } else if (name.equals("window")){
 694                     pw.println("\t\tret += \"\"+\"window = \" + getWindow(get_window()) + \", \";");
 695                 } else if (type == AtomicType.TYPE_ARRAY) {
 696                     pw.print("\t\tret += \"{\"");
 697                     for (int i = 0; i < tp.getArrayLength(); i++) {
 698                         pw.print(" + get_" + name + "(" + i + ") + \" \"");
 699                     }
 700                     pw.println(" + \"}\";");
 701                 } else {
 702                     pw.println("\t\tret += \"\"+\"" + name +" = \" + get_"+ name+"() +\", \";");
 703                 }
 704             }
 705 
 706         }
 707         pw.println("\t\treturn ret;\n\t}\n\n");
 708     }
 709 
 710     public void writeStubs(StructType stp, PrintWriter pw) {
 711         int type;
 712         String prefix = "";
 713         if (!stp.getIsInterface()) {
 714             prefix = "\t\tabstract ";
 715         } else {
 716             prefix = "\t";
 717         }
 718         for (Enumeration e = stp.getMembers() ; e.hasMoreElements() ;) {
 719             AtomicType tp = (AtomicType) e.nextElement();
 720 
 721             type = tp.getType();
 722             String name = tp.getName().replace('.','_');
 723             if ((name != null) && (name.length() > 0))
 724             {
 725                 if (type == AtomicType.TYPE_ARRAY) {
 726                     // Returns pointer to the start of the array
 727                     pw.println(prefix + "long get_" +name +"();");
 728 
 729                     pw.println(prefix + tp.getJavaType() + " get_" +name +"(int index);");
 730                     pw.println(prefix + "void set_" +name +"(int index, " + tp.getJavaType() + " v);");
 731                 } else {
 732                     pw.println(prefix + tp.getJavaType() + " get_" +name +"();");
 733                     if (type != AtomicType.TYPE_STRUCT) pw.println(prefix + "void set_" +name +"(" + tp.getJavaType() + " v);");
 734                 }
 735             }
 736         }
 737     }
 738 
 739     private int padSize(int size, int wordLength) {
 740         int bytesPerWord = wordLength / 8;
 741         // Make size dividable by bytesPerWord
 742         return (size + bytesPerWord / 2) / bytesPerWord * bytesPerWord;
 743     }
 744 
 745     public void writeAccessorImpls(StructType stp, PrintWriter pw) {
 746         int type;
 747         int i=0;
 748         String s_size_32 = getStructSize(stp, false);
 749         String s_size_64 = getStructSize(stp, true);
 750         int acc_size_32 = 0;
 751         int acc_size_64 = 0;
 752         String s_log = (generateLog?"log.finest(\"\");":"");
 753         for (Enumeration e = stp.getMembers() ; e.hasMoreElements() ;) {
 754             AtomicType tp = (AtomicType) e.nextElement();
 755 
 756             type = tp.getType();
 757             String name = tp.getName().replace('.','_');
 758             String pref = "\tpublic " ;
 759             if ((name != null) && (name.length() > 0))
 760             {
 761                 String jt = tp.getJavaType();
 762                 String ja_32 = tp.getJavaAccess(false);
 763                 String ja_64 = tp.getJavaAccess(true);
 764                 String ja = ja_32;
 765                 int elemSize_32 = AtomicType.getNativeSizeForAccess(ja_32);
 766                 int elemSize_64 = AtomicType.getNativeSizeForAccess(ja_64);
 767                 String elemSize = tp.getItemSize();
 768                 if (type == AtomicType.TYPE_ARRAY) {
 769                     acc_size_32 += elemSize_32 * tp.getArrayLength();
 770                     acc_size_64 += elemSize_64 * tp.getArrayLength();
 771                     pw.println(pref + tp.getJavaType() + " get_" +name + "(int index) { " +s_log+"return " +
 772                                tp.getJavaResult(stp.getOffset(tp) + "+index*" + elemSize, null) + "; }");
 773                     if (tp.getReferencedType() instanceof AtomicType) { // Set for StructType is forbidden
 774                         pw.println(MessageFormat.format(pref + "void set_{0}(int index, {1} v) '{' {3} {2}; '}'",
 775                                                         new Object[] {
 776                                                             name, jt,
 777                                                             tp.getJavaConversion("pData+"+stp.getOffset(tp)+" + index*" + elemSize, "v"),
 778                                                             s_log}));
 779                     }
 780                     // Returns pointer to the start of the array
 781                     pw.println(pref + "long get_" +name+ "() { "+s_log+"return pData+"+stp.getOffset(tp)+"; }");
 782                 } else if (type == AtomicType.TYPE_PTR) {
 783                     pw.println(MessageFormat.format(pref + "{0} get_{1}(int index) '{' {3} return {2}; '}'",
 784                                          new Object[] {
 785                                              jt, name,
 786                                              tp.getJavaResult("index*" + elemSize, "Native.getLong(pData+"+stp.getOffset(tp)+")"),
 787                                              s_log
 788                                              }));
 789                     pw.println(pref + "long get_" +name+ "() { "+s_log+"return Native.getLong(pData+"+stp.getOffset(tp)+"); }");
 790                     pw.println(MessageFormat.format(pref + "void set_{0}({1} v) '{' {3} {2}; '}'",
 791                                                     new Object[] {name, "long", "Native.putLong(pData + " + stp.getOffset(tp) + ", v)", s_log}));
 792                     acc_size_32 += elemSize_32;
 793                     acc_size_64 += elemSize_64;
 794                 } else {
 795                     acc_size_32 += elemSize_32;
 796                     acc_size_64 += elemSize_64;
 797                     pw.println(pref + tp.getJavaType() + " get_" +name +
 798                                "() { "+s_log+"return " + tp.getJavaResult(stp.getOffset(tp), null) + "; }");
 799                     if (type != AtomicType.TYPE_STRUCT) {
 800                         pw.println(MessageFormat.format(pref + "void set_{0}({1} v) '{' {3} {2}; '}'",
 801                                                         new Object[] {name, jt, tp.getJavaConversion("pData+"+stp.getOffset(tp), "v"), s_log}));
 802                     }
 803                 }
 804                 i++;
 805             }
 806         }
 807         if (s_size_32 != null && !s_size_32.equals(Integer.toString(acc_size_32))) {
 808             if (log.isLoggable(Level.FINE)) {
 809                 log.fine("32 bits: The size of the structure " + stp.getName() + " " + s_size_32 +
 810                         " is not equal to the accumulated size " +acc_size_32 + " of the fields");
 811             }
 812         } else if (s_size_64 != null && !s_size_64.equals(Integer.toString(acc_size_64))) {
 813             if (log.isLoggable(Level.FINE)) {
 814                 log.fine("64 bits: The size of the structure " + stp.getName() + " " +s_size_64+
 815                         " is not equal to the accumulated size " +acc_size_64+" of the fields");
 816             }
 817         }
 818     }
 819 
 820     public void writeWrapperSubclass(StructType stp, PrintWriter pw, boolean wide) {
 821 
 822 
 823         pw.println("class " + stp.getJavaClassName() + "AccessorImpl"  + " extends " + stp.getJavaClassName() + "Accessor  {");
 824         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 */");
 825 
 826         writeAccessorImpls(stp, pw);
 827 
 828         pw.println("\n\n } \n\n");
 829     }
 830 
 831     public void writeWrapper(String outputDir, StructType stp)
 832     {
 833         if (stp.getNumFields() > 0) {
 834 
 835             try {
 836                 FileOutputStream fs =  new FileOutputStream(outputDir + "/"+stp.getJavaClassName()+".java");
 837                 PrintWriter pw = new PrintWriter(fs);
 838                 pw.println("// This file is an automatically generated file, please do not edit this file, modify the WrapperGenerator.java file instead !\n" );
 839 
 840                 pw.println("package "+package_name+";\n");
 841                 pw.println("import sun.misc.*;\n");
 842                 pw.println("import java.util.logging.*;");
 843                 String baseClass = stp.getBaseClass();
 844                 if (baseClass == null) {
 845                     baseClass = defaultBaseClass;
 846                 }
 847                 if (stp.getIsInterface()) {
 848                     pw.print("public interface ");
 849                     pw.print(stp.getJavaClassName());
 850                 } else {
 851                     pw.print("public class ");
 852                     pw.print(stp.getJavaClassName() + " extends " + baseClass);
 853                 }
 854                 if (stp.getInterfaces() != null) {
 855                     pw.print(" implements " + stp.getInterfaces());
 856                 }
 857                 pw.println(" { ");
 858                 if (!stp.getIsInterface()) {
 859                     pw.println("\tprivate Unsafe unsafe = XlibWrapper.unsafe; ");
 860                     pw.println("\tprivate final boolean should_free_memory;");
 861                     pw.println("\tpublic static int getSize() { return " + stp.getSize() + "; }");
 862                     pw.println("\tpublic int getDataSize() { return getSize(); }");
 863                     pw.println("\n\tlong pData;");
 864                     pw.println("\n\tpublic long getPData() { return pData; }");
 865 
 866                     pw.println("\n\n\tpublic " + stp.getJavaClassName() + "(long addr) {");
 867                     if (generateLog) {
 868                         pw.println("\t\tlog.finest(\"Creating\");");
 869                     }
 870                     pw.println("\t\tpData=addr;");
 871                     pw.println("\t\tshould_free_memory = false;");
 872                     pw.println("\t}");
 873                     pw.println("\n\n\tpublic " + stp.getJavaClassName() + "() {");
 874                     if (generateLog) {
 875                         pw.println("\t\tlog.finest(\"Creating\");");
 876                     }
 877                     pw.println("\t\tpData = unsafe.allocateMemory(getSize());");
 878                     pw.println("\t\tshould_free_memory = true;");
 879                     pw.println("\t}");
 880 
 881                     pw.println("\n\n\tpublic void dispose() {");
 882                     if (generateLog) {
 883                         pw.println("\t\tlog.finest(\"Disposing\");");
 884                     }
 885                     pw.println("\t\tif (should_free_memory) {");
 886                     if (generateLog) {
 887                         pw.println("\t\t\tlog.finest(\"freeing memory\");");
 888                     }
 889                     pw.println("\t\t\tunsafe.freeMemory(pData); \n\t}");
 890                     pw.println("\t\t}");
 891                     writeAccessorImpls(stp, pw);
 892                     writeToString(stp,pw);
 893                 } else {
 894                     pw.println("\n\n\tvoid dispose();");
 895                     pw.println("\n\tlong getPData();");
 896                     writeStubs(stp,pw);
 897                 }
 898 
 899 
 900                 pw.println("}\n\n\n");
 901                 pw.close();
 902             }
 903             catch (Exception e)
 904             {
 905                 e.printStackTrace();
 906             }
 907         }
 908     }
 909 
 910     private boolean readSizeInfo(InputStream is, boolean wide) {
 911         String line;
 912         String splits[];
 913         BufferedReader in  = new BufferedReader(new InputStreamReader(is));
 914         try {
 915             while ((line = in.readLine()) != null)
 916             {
 917                 splits = line.split("\\p{Space}");
 918                 if (splits.length == 2)
 919                 {
 920                     if (wide) {
 921                         sizeTable64bit.put(splits[0],splits[1]);
 922                     } else {
 923                         sizeTable32bit.put(splits[0],splits[1]);
 924                     }
 925                 }
 926             }
 927             return true;
 928         } catch (Exception e) {
 929             e.printStackTrace();
 930             return false;
 931         }
 932     }
 933 
 934     public void writeFunctionCallWrapper(String outputDir, FunctionType ft) {
 935         try {
 936             FileOutputStream fs =  new FileOutputStream(outputDir + "/" + ft.getName()+".java");
 937             PrintWriter pw = new PrintWriter(fs);
 938             pw.println("// This file is an automatically generated file, please do not edit this file, modify the WrapperGenerator.java file instead !\n" );
 939 
 940             pw.println("package "+package_name+";\n");
 941             pw.println("import sun.misc.Unsafe;\n");
 942             pw.println("class " + ft.getName() + " {");
 943             pw.println("\tprivate static Unsafe unsafe = XlibWrapper.unsafe;");
 944             pw.println("\tprivate boolean __executed = false;");
 945             pw.println("\tprivate boolean __disposed = false;");
 946             Iterator iter = ft.getArguments().iterator();
 947             while (iter.hasNext()) {
 948                 AtomicType at = (AtomicType)iter.next();
 949                 if (at.isIn()) {
 950                     pw.println("\t" + at.getJavaType() + " _" + at.getName() + ";");
 951                 } else {
 952                     pw.println("\tlong " + at.getName() + "_ptr = unsafe.allocateMemory(Native.get" + at.getTypeUpperCase() + "Size());");
 953                 }
 954             }
 955             pw.println("\tpublic " + ft.getName() + "(");
 956             iter = ft.getArguments().iterator();
 957             boolean first = true;
 958             while (iter.hasNext()) {
 959                 AtomicType at = (AtomicType)iter.next();
 960                 if (at.isIn() || at.isInOut()) {
 961                     if (!first) {
 962                         pw.println(",");
 963                     }
 964                     first = false;
 965                     pw.print("\t\t" + at.getJavaType() + " " + at.getName());
 966                 }
 967             }
 968             pw.println("\t)");
 969             pw.println("\t{");
 970             iter = ft.getArguments().iterator();
 971             while (iter.hasNext()) {
 972                 AtomicType at = (AtomicType)iter.next();
 973                 if (at.isIn() || at.isInOut()) {
 974                     pw.println("\t\tset_" + at.getName() + "(" + at.getName() + ");");
 975                 }
 976             }
 977             pw.println("\t}");
 978 
 979             pw.println("\tpublic " + ft.getReturnType() + " execute() {");
 980             if (ft.isVoid()) {
 981                 pw.println("\t\texecute(null);");
 982             } else {
 983                 pw.println("\t\treturn execute(null);");
 984             }
 985             pw.println("\t}");
 986 
 987             pw.println("\tpublic " + ft.getReturnType() + " execute(XToolkit.XErrorHandler errorHandler) {");
 988             pw.println("\t\tif (__disposed) {");
 989             pw.println("\t\t    throw new IllegalStateException(\"Disposed\");");
 990             pw.println("\t\t}");
 991             pw.println("\t\tXToolkit.awtLock();");
 992             pw.println("\t\ttry {");
 993             pw.println("\t\t\tif (__executed) {");
 994             pw.println("\t\t\t    throw new IllegalStateException(\"Already executed\");");
 995             pw.println("\t\t\t}");
 996             pw.println("\t\t\t__executed = true;");
 997             pw.println("\t\t\tif (errorHandler != null) {");
 998             pw.println("\t\t\t    XToolkit.WITH_XERROR_HANDLER(errorHandler);");
 999             pw.println("\t\t\t}");
1000             iter = ft.getArguments().iterator();
1001             while (iter.hasNext()) {
1002                 AtomicType at = (AtomicType)iter.next();
1003                 if (!at.isIn() && at.isAutoFree()) {
1004                     pw.println("\t\t\tNative.put" + at.getTypeUpperCase() + "(" +at.getName() + "_ptr, 0);");
1005                 }
1006             }
1007             if (!ft.isVoid()) {
1008                 pw.println("\t\t\t" + ft.getReturnType() + " status = ");
1009             }
1010             pw.println("\t\t\tXlibWrapper." + ft.getName() + "(XToolkit.getDisplay(), ");
1011             iter = ft.getArguments().iterator();
1012             first = true;
1013             while (iter.hasNext()) {
1014                 AtomicType at = (AtomicType)iter.next();
1015                 if (!first) {
1016                     pw.println(",");
1017                 }
1018                 first = false;
1019                 if (at.isIn()) {
1020                     pw.print("\t\t\t\tget_" + at.getName() + "()");
1021                 } else {
1022                     pw.print("\t\t\t\t" + at.getName() + "_ptr");
1023                 }
1024             }
1025             pw.println("\t\t\t);");
1026             pw.println("\t\t\tif (errorHandler != null) {");
1027             pw.println("\t\t\t    XToolkit.RESTORE_XERROR_HANDLER();");
1028             pw.println("\t\t\t}");
1029             if (!ft.isVoid()) {
1030                 pw.println("\t\t\treturn status;");
1031             }
1032             pw.println("\t\t} finally {");
1033             pw.println("\t\t    XToolkit.awtUnlock();");
1034             pw.println("\t\t}");
1035             pw.println("\t}");
1036 
1037             pw.println("\tpublic boolean isExecuted() {");
1038             pw.println("\t    return __executed;");
1039             pw.println("\t}");
1040             pw.println("\t");
1041             pw.println("\tpublic boolean isDisposed() {");
1042             pw.println("\t    return __disposed;");
1043             pw.println("\t}");
1044             pw.println("\tpublic void finalize() {");
1045             pw.println("\t    dispose();");
1046             pw.println("\t}");
1047 
1048             pw.println("\tpublic void dispose() {");
1049             pw.println("\t\tXToolkit.awtLock();");
1050             pw.println("\t\ttry {");
1051             pw.println("\t\tif (__disposed || !__executed) {");
1052             pw.println("\t\t    return;");
1053             pw.println("\t\t} finally {");
1054             pw.println("\t\t    XToolkit.awtUnlock();");
1055             pw.println("\t\t}");
1056             pw.println("\t\t}");
1057 
1058             iter = ft.getArguments().iterator();
1059             while (iter.hasNext()) {
1060                 AtomicType at = (AtomicType)iter.next();
1061                 if (!at.isIn()) {
1062                     if (at.isAutoFree()) {
1063                         pw.println("\t\tif (__executed && get_" + at.getName() + "()!= 0) {");
1064                         pw.println("\t\t\tXlibWrapper.XFree(get_" + at.getName() + "());");
1065                         pw.println("\t\t}");
1066                     }
1067                     pw.println("\t\tunsafe.freeMemory(" + at.getName() + "_ptr);");
1068                 }
1069             }
1070             pw.println("\t\t__disposed = true;");
1071             pw.println("\t\t}");
1072             pw.println("\t}");
1073 
1074             iter = ft.getArguments().iterator();
1075             while (iter.hasNext()) {
1076                 AtomicType at = (AtomicType)iter.next();
1077                 pw.println("\tpublic " + at.getJavaType() + " get_" + at.getName() + "() {");
1078 
1079                 pw.println("\t\tif (__disposed) {");
1080                 pw.println("\t\t    throw new IllegalStateException(\"Disposed\");");
1081                 pw.println("\t\t}");
1082                 pw.println("\t\tif (!__executed) {");
1083                 pw.println("\t\t    throw new IllegalStateException(\"Not executed\");");
1084                 pw.println("\t\t}");
1085 
1086                 if (at.isIn()) {
1087                     pw.println("\t\treturn _" + at.getName() + ";");
1088                 } else {
1089                     pw.println("\t\treturn Native.get" + at.getTypeUpperCase() + "(" + at.getName() + "_ptr);");
1090                 }
1091                 pw.println("\t}");
1092 
1093                 pw.println("\tpublic void set_" + at.getName() + "(" + at.getJavaType() + " data) {");
1094                 if (at.isIn()) {
1095                     pw.println("\t\t_" + at.getName() + " = data;");
1096                 } else {
1097                     pw.println("\t\tNative.put" + at.getTypeUpperCase() + "(" + at.getName() + "_ptr, data);");
1098                 }
1099                 pw.println("\t}");
1100             }
1101             pw.println("}");
1102             pw.close();
1103         } catch (Exception e) {
1104             e.printStackTrace();
1105         }
1106     }
1107 
1108     public void writeJavaWrapperClass(String outputDir) {
1109 //          (new File(outputDir, package_path)).mkdirs();
1110         try {
1111             for (Enumeration e = symbolTable.elements() ; e.hasMoreElements() ;) {
1112                 BaseType tp = (BaseType) e.nextElement();
1113                 if (tp instanceof StructType) {
1114                     StructType st = (StructType) tp;
1115                     writeWrapper(outputDir, st);
1116                 } else if (tp instanceof FunctionType) {
1117                     writeFunctionCallWrapper(outputDir, (FunctionType)tp);
1118                 }
1119             }
1120         }
1121         catch (Exception e) {
1122             e.printStackTrace();
1123         }
1124     }
1125 
1126 
1127     public void writeNativeSizer(String file)
1128     {
1129         int type;
1130         int i=0;
1131         int j=0;
1132         BaseType tp;
1133         StructType stp;
1134         Enumeration eo;
1135 
1136 
1137         try {
1138 
1139             FileOutputStream fs =  new FileOutputStream(file);
1140             PrintWriter pw = new PrintWriter(fs);
1141 
1142             pw.println("/* This file is an automatically generated file, please do not edit this file, modify the XlibParser.java file instead !*/\n" );
1143             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");
1144             pw.println("#include <X11/extensions/Xdbe.h>");
1145             pw.println("#include <X11/XKBlib.h>");
1146             pw.println("#include \"awt_p.h\"");
1147             pw.println("#include \"color.h\"");
1148             pw.println("#include \"colordata.h\"");
1149             pw.println("\ntypedef struct\n");
1150             pw.println("{\n");
1151             pw.println("    unsigned long flags;\n");
1152             pw.println("    unsigned long functions;\n");
1153             pw.println("    unsigned long decorations;\n");
1154             pw.println("    long inputMode;\n");
1155             pw.println("    unsigned long status;\n");
1156             pw.println("} PropMwmHints;\n");
1157 
1158             pw.println("\n\nint main(){");
1159             j=0;
1160             for ( eo = symbolTable.elements() ; eo.hasMoreElements() ;) {
1161                 tp = (BaseType) eo.nextElement();
1162                 if (tp instanceof StructType)
1163                 {
1164                     stp = (StructType) tp;
1165                     if (!stp.getIsInterface()) {
1166                         pw.println(stp.getName()+"  temp"+ j + ";\n");
1167                         j++;
1168                     }
1169                 }
1170             }
1171             j=0;
1172 
1173             pw.println("printf(\"long\t%d\\n\",(int)sizeof(long));");
1174             pw.println("printf(\"int\t%d\\n\",(int)sizeof(int));");
1175             pw.println("printf(\"short\t%d\\n\",(int)sizeof(short));");
1176             pw.println("printf(\"ptr\t%d\\n\",(int)sizeof(void *));");
1177             pw.println("printf(\"Bool\t%d\\n\",(int)sizeof(Bool));");
1178             pw.println("printf(\"Atom\t%d\\n\",(int)sizeof(Atom));");
1179             pw.println("printf(\"Window\t%d\\n\",(int)sizeof(Window));");
1180 
1181 
1182             for (eo = symbolTable.elements() ; eo.hasMoreElements() ;) {
1183 
1184 
1185                 tp = (BaseType) eo.nextElement();
1186                 if (tp instanceof StructType)
1187                 {
1188                     stp = (StructType) tp;
1189                     if (stp.getIsInterface()) {
1190                         continue;
1191                     }
1192                     for (Enumeration e = stp.getMembers() ; e.hasMoreElements() ;) {
1193                         AtomicType atp = (AtomicType) e.nextElement();
1194                         if (atp.isAlias()) continue;
1195                         pw.println("printf(\""+ stp.getName() + "." + atp.getName() + "\t%d\\n\""+
1196                                    ",(int)((unsigned long ) &temp"+j+"."+atp.getName()+"- (unsigned long ) &temp" + j + ")  );");
1197 
1198                         i++;
1199 
1200 
1201                     }
1202                     pw.println("printf(\""+ stp.getName() + "\t%d\\n\"" + ",(int)sizeof(temp"+j+"));");
1203 
1204                     j++;
1205                 }
1206 
1207             }
1208             pw.println("return 0;");
1209             pw.println("}");
1210             pw.close();
1211 
1212         }
1213         catch (Exception e)
1214         {
1215             e.printStackTrace();
1216         }
1217     }
1218 
1219     private void initTypes() {
1220         symbolTable.put("int", new AtomicType(AtomicType.TYPE_INT, "", "int"));
1221         symbolTable.put("short", new AtomicType(AtomicType.TYPE_SHORT, "", "short"));
1222         symbolTable.put("long", new AtomicType(AtomicType.TYPE_LONG, "", "long"));
1223         symbolTable.put("float", new AtomicType(AtomicType.TYPE_FLOAT, "", "float"));
1224         symbolTable.put("double", new AtomicType(AtomicType.TYPE_DOUBLE, "", "double"));
1225         symbolTable.put("Bool", new AtomicType(AtomicType.TYPE_BOOL, "", "Bool"));
1226         symbolTable.put("char", new AtomicType(AtomicType.TYPE_CHAR, "", "char"));
1227         symbolTable.put("byte", new AtomicType(AtomicType.TYPE_BYTE, "", "byte"));
1228         symbolTable.put("pointer", new AtomicType(AtomicType.TYPE_PTR, "", "pointer"));
1229         symbolTable.put("longlong", new AtomicType(AtomicType.TYPE_LONG_LONG, "", "longlong"));
1230         symbolTable.put("Atom", new AtomicType(AtomicType.TYPE_ATOM, "", "Atom"));
1231         symbolTable.put("ulong", new AtomicType(AtomicType.TYPE_ULONG, "", "ulong"));
1232     }
1233     public WrapperGenerator(String outputDir, String xlibFilename) {
1234         initTypes();
1235         try {
1236             BufferedReader in  = new BufferedReader(new FileReader(xlibFilename));
1237             String line;
1238             String splits[];
1239             BaseType curType = null;
1240             while ((line = in.readLine()) != null)
1241             {
1242                 int commentStart = line.indexOf("//");
1243                 if (commentStart >= 0) {
1244                     // remove comment
1245                     line = line.substring(0, commentStart);
1246                 }
1247 
1248                 if ("".equals(line)) {
1249                     // skip empty line
1250                     continue;
1251                 }
1252 
1253                 splits = line.split("\\p{Space}+");
1254                 if (splits.length >= 2)
1255                 {
1256                     String struct_name = curType.getName();
1257                     String field_name = splits[1];
1258                     String s_type = splits[2];
1259                     BaseType bt = curType;
1260                     int type = AtomicType.getTypeForString(s_type);
1261                     AtomicType atp = null;
1262                     if (bt != null && type != -1) {
1263                         atp = new AtomicType(type,field_name,s_type);
1264                         if (splits.length > 3) {
1265                             atp.setAttributes(splits);
1266                         }
1267                         if (bt instanceof StructType) {
1268                             StructType  stp = (StructType) bt;
1269                             stp.addMember(atp);
1270                         } else if (bt instanceof FunctionType) {
1271                             ((FunctionType)bt).addArgument(atp);
1272                         }
1273                     }
1274                     else if (bt == null) {
1275                         System.out.println("Cannot find " + struct_name);
1276                     }
1277 
1278                 }
1279                 else  if (line != null) {
1280                     BaseType bt = (BaseType) symbolTable.get(line);
1281                     if (bt == null) {
1282                         if (line.startsWith("!")) {
1283                             FunctionType ft = new FunctionType(line);
1284                             ft.setName(line);
1285                             symbolTable.put(ft.getName(),ft);
1286                             curType = ft;
1287                         } else {
1288                             StructType stp = new StructType(line);
1289                             stp.setName(line);
1290                             curType = stp;
1291                             symbolTable.put(stp.getName(),stp);
1292                         }
1293                     }
1294                 }
1295 
1296             }
1297             in.close();
1298         }
1299         catch (Exception e) {
1300             e.printStackTrace();
1301         }
1302 
1303     }
1304     private void makeSizer(String outputDir) {
1305         if (wide) {
1306             sizerFileName = "sizer.64.c";
1307         } else {
1308             sizerFileName = "sizer.32.c";
1309         }
1310         File fp = new File(outputDir, sizerFileName);
1311         writeNativeSizer(fp.getAbsolutePath());
1312     }
1313     private boolean readSizeInfo(String sizeInfo) {
1314         try {
1315             File f = new File(sizeInfo+".32");
1316             boolean res = true;
1317             FileInputStream fis = null;
1318             if (f.exists()) {
1319                 fis = new FileInputStream(f);
1320                 res = readSizeInfo(fis, false);
1321                 fis.close();
1322             }
1323             f = new File(sizeInfo+".64");
1324             if (f.exists()) {
1325                 fis = new FileInputStream(f);
1326                 res &= readSizeInfo(fis, true);
1327                 fis.close();
1328             }
1329             return res;
1330         } catch (Exception e) {
1331             e.printStackTrace();
1332             return false;
1333         }
1334     }
1335 
1336     private void startGeneration(String outputDir, String sizeInfo) {
1337         if (readSizeInfo(sizeInfo))
1338         {
1339             writeJavaWrapperClass(outputDir);
1340         }
1341         else {
1342             System.out.println("Error calculating offsets");
1343         }
1344     }
1345 
1346     public static void main(String[] args) {
1347 
1348         if (args.length < 4) {
1349             System.out.println("Usage:\nWrapperGenerator <output_dir> <xlibtypes.txt> <action> [<platform> | <sizes info file>]");
1350             System.out.println("Where <action>: gen, sizer");
1351             System.out.println("      <platform>: 32, 64");
1352             System.exit(1);
1353         }
1354 
1355         WrapperGenerator xparser = new WrapperGenerator(args[0], args[1]);
1356         if (args[2].equals("sizer")) {
1357             xparser.wide = args[3].equals("64");
1358             xparser.makeSizer(args[0]);
1359         } else if (args[2].equals("gen")) {
1360             xparser.startGeneration(args[0], args[3]);
1361         }
1362     }
1363 
1364 }