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