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