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