< prev index next >

make/jdk/src/classes/build/tools/x11wrappergen/WrapperGenerator.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1092                     pw.println("\t\treturn Native.get" + at.getTypeUpperCase() + "(" + at.getName() + "_ptr);");
1093                 }
1094                 pw.println("\t}");
1095 
1096                 pw.println("\tpublic void set_" + at.getName() + "(" + at.getJavaType() + " data) {");
1097                 if (at.isIn()) {
1098                     pw.println("\t\t_" + at.getName() + " = data;");
1099                 } else {
1100                     pw.println("\t\tNative.put" + at.getTypeUpperCase() + "(" + at.getName() + "_ptr, data);");
1101                 }
1102                 pw.println("\t}");
1103             }
1104             pw.println("}");
1105             pw.close();
1106         } catch (Exception e) {
1107             e.printStackTrace();
1108         }
1109     }
1110 
1111     public void writeJavaWrapperClass(String outputDir) {
1112 //          (new File(outputDir, package_path)).mkdirs();
1113         try {
1114             for (Enumeration e = symbolTable.elements() ; e.hasMoreElements() ;) {
1115                 BaseType tp = (BaseType) e.nextElement();
1116                 if (tp instanceof StructType) {
1117                     StructType st = (StructType) tp;
1118                     writeWrapper(outputDir, st);
1119                 } else if (tp instanceof FunctionType) {
1120                     writeFunctionCallWrapper(outputDir, (FunctionType)tp);
1121                 }
1122             }
1123         }
1124         catch (Exception e) {
1125             e.printStackTrace();
1126         }
1127     }
1128 
1129 
1130     public void writeNativeSizer(String file)
1131     {
1132         int type;
1133         int i=0;
1134         int j=0;
1135         BaseType tp;
1136         StructType stp;
1137         Enumeration eo;
1138 
1139 
1140         try {
1141 
1142             FileOutputStream fs =  new FileOutputStream(file);
1143             PrintWriter pw = new PrintWriter(fs);
1144 
1145             pw.println("/* This file is an automatically generated file, please do not edit this file, modify the XlibParser.java file instead !*/\n" );
1146             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");
1147             pw.println("#include <X11/extensions/Xdbe.h>");
1148             pw.println("#include <X11/XKBlib.h>");
1149             pw.println("#include \"awt_p.h\"");
1150             pw.println("#include \"color.h\"");
1151             pw.println("#include \"colordata.h\"");
1152             pw.println("\ntypedef struct\n");
1153             pw.println("{\n");
1154             pw.println("    unsigned long flags;\n");
1155             pw.println("    unsigned long functions;\n");
1156             pw.println("    unsigned long decorations;\n");
1157             pw.println("    long inputMode;\n");
1158             pw.println("    unsigned long status;\n");
1159             pw.println("} PropMwmHints;\n");
1160 
1161 
1162             pw.println("\n\nint main(){");
1163             j=0;
1164             for ( eo = symbolTable.elements() ; eo.hasMoreElements() ;) {
1165                 tp = (BaseType) eo.nextElement();
1166                 if (tp instanceof StructType)
1167                 {
1168                     stp = (StructType) tp;
1169                     if (!stp.getIsInterface()) {
1170                         pw.println(stp.getName()+"  temp"+ j + ";\n");
1171                         j++;
1172                     }
1173                 }
1174             }
1175             j=0;
1176 
1177             pw.println("printf(\"long\t%d\\n\",(int)sizeof(long));");
1178             pw.println("printf(\"int\t%d\\n\",(int)sizeof(int));");
1179             pw.println("printf(\"short\t%d\\n\",(int)sizeof(short));");
1180             pw.println("printf(\"ptr\t%d\\n\",(int)sizeof(void *));");
1181             pw.println("printf(\"Bool\t%d\\n\",(int)sizeof(Bool));");
1182             pw.println("printf(\"Atom\t%d\\n\",(int)sizeof(Atom));");
1183             pw.println("printf(\"Window\t%d\\n\",(int)sizeof(Window));");
1184 
1185 
1186             for (eo = symbolTable.elements() ; eo.hasMoreElements() ;) {
1187 
1188 
1189                 tp = (BaseType) eo.nextElement();
1190                 if (tp instanceof StructType)
1191                 {
1192                     stp = (StructType) tp;
1193                     if (stp.getIsInterface()) {
1194                         continue;
1195                     }
1196                     for (Enumeration e = stp.getMembers() ; e.hasMoreElements() ;) {
1197                         AtomicType atp = (AtomicType) e.nextElement();
1198                         if (atp.isAlias()) continue;
1199                         pw.println("printf(\""+ stp.getName() + "." + atp.getName() + "\t%d\\n\""+
1200                                    ",(int)((unsigned long ) &temp"+j+"."+atp.getName()+"- (unsigned long ) &temp" + j + ")  );");
1201 
1202                         i++;
1203 
1204 
1205                     }


1217         catch (Exception e)
1218         {
1219             e.printStackTrace();
1220         }
1221     }
1222 
1223     private void initTypes() {
1224         symbolTable.put("int", new AtomicType(AtomicType.TYPE_INT, "", "int"));
1225         symbolTable.put("short", new AtomicType(AtomicType.TYPE_SHORT, "", "short"));
1226         symbolTable.put("long", new AtomicType(AtomicType.TYPE_LONG, "", "long"));
1227         symbolTable.put("float", new AtomicType(AtomicType.TYPE_FLOAT, "", "float"));
1228         symbolTable.put("double", new AtomicType(AtomicType.TYPE_DOUBLE, "", "double"));
1229         symbolTable.put("Bool", new AtomicType(AtomicType.TYPE_BOOL, "", "Bool"));
1230         symbolTable.put("char", new AtomicType(AtomicType.TYPE_CHAR, "", "char"));
1231         symbolTable.put("byte", new AtomicType(AtomicType.TYPE_BYTE, "", "byte"));
1232         symbolTable.put("pointer", new AtomicType(AtomicType.TYPE_PTR, "", "pointer"));
1233         symbolTable.put("longlong", new AtomicType(AtomicType.TYPE_LONG_LONG, "", "longlong"));
1234         symbolTable.put("Atom", new AtomicType(AtomicType.TYPE_ATOM, "", "Atom"));
1235         symbolTable.put("ulong", new AtomicType(AtomicType.TYPE_ULONG, "", "ulong"));
1236     }
1237     public WrapperGenerator(String outputDir, String xlibFilename) {

1238         initTypes();
1239         try {
1240             BufferedReader in  = new BufferedReader(new FileReader(xlibFilename));
1241             String line;
1242             String splits[];
1243             BaseType curType = null;
1244             while ((line = in.readLine()) != null)
1245             {
1246                 int commentStart = line.indexOf("//");
1247                 if (commentStart >= 0) {
1248                     // remove comment
1249                     line = line.substring(0, commentStart);
1250                 }
1251 
1252                 if ("".equals(line)) {
1253                     // skip empty line
1254                     continue;
1255                 }
1256 
1257                 splits = line.split("\\p{Space}+");


1286                         if (line.startsWith("!")) {
1287                             FunctionType ft = new FunctionType(line);
1288                             ft.setName(line);
1289                             symbolTable.put(ft.getName(),ft);
1290                             curType = ft;
1291                         } else {
1292                             StructType stp = new StructType(line);
1293                             stp.setName(line);
1294                             curType = stp;
1295                             symbolTable.put(stp.getName(),stp);
1296                         }
1297                     }
1298                 }
1299 
1300             }
1301             in.close();
1302         }
1303         catch (Exception e) {
1304             e.printStackTrace();
1305         }
1306 
1307     }
1308     private void makeSizer(String outputDir) {
1309         if (wide) {
1310             sizerFileName = "sizer.64.c";
1311         } else {
1312             sizerFileName = "sizer.32.c";
1313         }
1314         File fp = new File(outputDir, sizerFileName);


1315         writeNativeSizer(fp.getAbsolutePath());
1316     }
1317     private boolean readSizeInfo(String sizeInfo) {

1318         try {
1319             File f = new File(sizeInfo+".32");
1320             boolean res = true;
1321             FileInputStream fis = null;
1322             if (f.exists()) {
1323                 fis = new FileInputStream(f);
1324                 res = readSizeInfo(fis, false);
1325                 fis.close();
1326             }
1327             f = new File(sizeInfo+".64");
1328             if (f.exists()) {
1329                 fis = new FileInputStream(f);
1330                 res &= readSizeInfo(fis, true);
1331                 fis.close();
1332             }
1333             return res;
1334         } catch (Exception e) {
1335             e.printStackTrace();
1336             return false;
1337         }
1338     }
1339 
1340     private void startGeneration(String outputDir, String sizeInfo) {
1341         if (readSizeInfo(sizeInfo))
1342         {
1343             writeJavaWrapperClass(outputDir);
1344         }
1345         else {
1346             System.out.println("Error calculating offsets");
1347         }
1348     }
1349 
1350     public static void main(String[] args) {
1351 
1352         if (args.length < 4) {
1353             System.out.println("Usage:\nWrapperGenerator <output_dir> <xlibtypes.txt> <action> [<platform> | <sizes info file>]");
1354             System.out.println("Where <action>: gen, sizer");
1355             System.out.println("      <platform>: 32, 64");


1356             System.exit(1);
1357         }
1358 
1359         WrapperGenerator xparser = new WrapperGenerator(args[0], args[1]);
1360         if (args[2].equals("sizer")) {
1361             xparser.wide = args[3].equals("64");
1362             xparser.makeSizer(args[0]);
1363         } else if (args[2].equals("gen")) {
1364             xparser.startGeneration(args[0], args[3]);

1365         }
1366     }
1367 
1368 }
   1 /*
   2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1092                     pw.println("\t\treturn Native.get" + at.getTypeUpperCase() + "(" + at.getName() + "_ptr);");
1093                 }
1094                 pw.println("\t}");
1095 
1096                 pw.println("\tpublic void set_" + at.getName() + "(" + at.getJavaType() + " data) {");
1097                 if (at.isIn()) {
1098                     pw.println("\t\t_" + at.getName() + " = data;");
1099                 } else {
1100                     pw.println("\t\tNative.put" + at.getTypeUpperCase() + "(" + at.getName() + "_ptr, data);");
1101                 }
1102                 pw.println("\t}");
1103             }
1104             pw.println("}");
1105             pw.close();
1106         } catch (Exception e) {
1107             e.printStackTrace();
1108         }
1109     }
1110 
1111     public void writeJavaWrapperClass(String outputDir) {

1112         try {
1113             for (Enumeration e = symbolTable.elements() ; e.hasMoreElements() ;) {
1114                 BaseType tp = (BaseType) e.nextElement();
1115                 if (tp instanceof StructType) {
1116                     StructType st = (StructType) tp;
1117                     writeWrapper(outputDir, st);
1118                 } else if (tp instanceof FunctionType) {
1119                     writeFunctionCallWrapper(outputDir, (FunctionType)tp);
1120                 }
1121             }
1122         }
1123         catch (Exception e) {
1124             e.printStackTrace();
1125         }
1126     }
1127 

1128     public void writeNativeSizer(String file)
1129     {
1130         int type;
1131         int i=0;
1132         int j=0;
1133         BaseType tp;
1134         StructType stp;
1135         Enumeration eo;
1136 

1137         try {
1138 
1139             FileOutputStream fs =  new FileOutputStream(file);
1140             PrintWriter pw = new PrintWriter(fs);
1141 
1142             pw.println("/* This file is an automatically generated file, please do not edit this file, modify the XlibParser.java file instead !*/\n" );
1143             pw.println("#include <X11/Xlib.h>\n#include <X11/Xutil.h>\n#include <X11/Xos.h>\n#include <X11/Xatom.h>\n#include <stdio.h>\n");
1144             pw.println("#include <X11/extensions/Xdbe.h>");
1145             pw.println("#include <X11/XKBlib.h>");
1146             pw.println("#include \"awt_p.h\"");
1147             pw.println("#include \"color.h\"");
1148             pw.println("#include \"colordata.h\"");
1149             pw.println("\ntypedef struct\n");
1150             pw.println("{\n");
1151             pw.println("    unsigned long flags;\n");
1152             pw.println("    unsigned long functions;\n");
1153             pw.println("    unsigned long decorations;\n");
1154             pw.println("    long inputMode;\n");
1155             pw.println("    unsigned long status;\n");
1156             pw.println("} PropMwmHints;\n");
1157 

1158             pw.println("\n\nint main(){");
1159             j=0;
1160             for ( eo = symbolTable.elements() ; eo.hasMoreElements() ;) {
1161                 tp = (BaseType) eo.nextElement();
1162                 if (tp instanceof StructType)
1163                 {
1164                     stp = (StructType) tp;
1165                     if (!stp.getIsInterface()) {
1166                         pw.println(stp.getName()+"  temp"+ j + ";\n");
1167                         j++;
1168                     }
1169                 }
1170             }
1171             j=0;
1172 
1173             pw.println("printf(\"long\t%d\\n\",(int)sizeof(long));");
1174             pw.println("printf(\"int\t%d\\n\",(int)sizeof(int));");
1175             pw.println("printf(\"short\t%d\\n\",(int)sizeof(short));");
1176             pw.println("printf(\"ptr\t%d\\n\",(int)sizeof(void *));");
1177             pw.println("printf(\"Bool\t%d\\n\",(int)sizeof(Bool));");
1178             pw.println("printf(\"Atom\t%d\\n\",(int)sizeof(Atom));");
1179             pw.println("printf(\"Window\t%d\\n\",(int)sizeof(Window));");
1180 

1181             for (eo = symbolTable.elements() ; eo.hasMoreElements() ;) {
1182 
1183 
1184                 tp = (BaseType) eo.nextElement();
1185                 if (tp instanceof StructType)
1186                 {
1187                     stp = (StructType) tp;
1188                     if (stp.getIsInterface()) {
1189                         continue;
1190                     }
1191                     for (Enumeration e = stp.getMembers() ; e.hasMoreElements() ;) {
1192                         AtomicType atp = (AtomicType) e.nextElement();
1193                         if (atp.isAlias()) continue;
1194                         pw.println("printf(\""+ stp.getName() + "." + atp.getName() + "\t%d\\n\""+
1195                                    ",(int)((unsigned long ) &temp"+j+"."+atp.getName()+"- (unsigned long ) &temp" + j + ")  );");
1196 
1197                         i++;
1198 
1199 
1200                     }


1212         catch (Exception e)
1213         {
1214             e.printStackTrace();
1215         }
1216     }
1217 
1218     private void initTypes() {
1219         symbolTable.put("int", new AtomicType(AtomicType.TYPE_INT, "", "int"));
1220         symbolTable.put("short", new AtomicType(AtomicType.TYPE_SHORT, "", "short"));
1221         symbolTable.put("long", new AtomicType(AtomicType.TYPE_LONG, "", "long"));
1222         symbolTable.put("float", new AtomicType(AtomicType.TYPE_FLOAT, "", "float"));
1223         symbolTable.put("double", new AtomicType(AtomicType.TYPE_DOUBLE, "", "double"));
1224         symbolTable.put("Bool", new AtomicType(AtomicType.TYPE_BOOL, "", "Bool"));
1225         symbolTable.put("char", new AtomicType(AtomicType.TYPE_CHAR, "", "char"));
1226         symbolTable.put("byte", new AtomicType(AtomicType.TYPE_BYTE, "", "byte"));
1227         symbolTable.put("pointer", new AtomicType(AtomicType.TYPE_PTR, "", "pointer"));
1228         symbolTable.put("longlong", new AtomicType(AtomicType.TYPE_LONG_LONG, "", "longlong"));
1229         symbolTable.put("Atom", new AtomicType(AtomicType.TYPE_ATOM, "", "Atom"));
1230         symbolTable.put("ulong", new AtomicType(AtomicType.TYPE_ULONG, "", "ulong"));
1231     }
1232 
1233     public WrapperGenerator(String xlibFilename) {
1234         initTypes();
1235         try {
1236             BufferedReader in  = new BufferedReader(new FileReader(xlibFilename));
1237             String line;
1238             String splits[];
1239             BaseType curType = null;
1240             while ((line = in.readLine()) != null)
1241             {
1242                 int commentStart = line.indexOf("//");
1243                 if (commentStart >= 0) {
1244                     // remove comment
1245                     line = line.substring(0, commentStart);
1246                 }
1247 
1248                 if ("".equals(line)) {
1249                     // skip empty line
1250                     continue;
1251                 }
1252 
1253                 splits = line.split("\\p{Space}+");


1282                         if (line.startsWith("!")) {
1283                             FunctionType ft = new FunctionType(line);
1284                             ft.setName(line);
1285                             symbolTable.put(ft.getName(),ft);
1286                             curType = ft;
1287                         } else {
1288                             StructType stp = new StructType(line);
1289                             stp.setName(line);
1290                             curType = stp;
1291                             symbolTable.put(stp.getName(),stp);
1292                         }
1293                     }
1294                 }
1295 
1296             }
1297             in.close();
1298         }
1299         catch (Exception e) {
1300             e.printStackTrace();
1301         }







1302     }
1303 
1304     private void makeSizer(String sizerFileName) {
1305         File fp = new File(sizerFileName);
1306         writeNativeSizer(fp.getAbsolutePath());
1307     }
1308 
1309     private boolean readFileSizeInfo(String filename, boolean wide) {
1310         try {

1311             boolean res = true;
1312             FileInputStream fis = new FileInputStream(filename);
1313             res = readSizeInfo(fis, wide);








1314             fis.close();

1315             return res;
1316         } catch (Exception e) {
1317             e.printStackTrace();
1318             return false;
1319         }
1320     }
1321 
1322     private void startGeneration(String outputDir, String filename, boolean wide) {
1323         if (readFileSizeInfo(filename, wide))
1324         {
1325             writeJavaWrapperClass(outputDir);
1326         }
1327         else {
1328             System.out.println("Error calculating offsets");
1329         }
1330     }
1331 
1332     public static void main(String[] args) {

1333         if (args.length < 4) {
1334             System.out.println("Usage:\nWrapperGenerator gen_java <output_dir> <xlibtypes.txt> <sizes-*.txt> <platform>");
1335             System.out.println("      or");
1336             System.out.println("WrapperGenerator gen_c_source <output_file> <xlibtypes.txt> <platform>");
1337             System.out.println("Where <platform>: 32, 64");
1338 
1339             System.exit(1);
1340         }
1341 
1342         WrapperGenerator xparser = new WrapperGenerator(args[2]);
1343         if (args[0].equals("gen_c_source")) {
1344             xparser.wide = args[3].equals("64");
1345             xparser.makeSizer(args[1]);
1346         } else if (args[0].equals("gen_java")) {
1347             boolean wide = args[4].equals("64");
1348             xparser.startGeneration(args[1], args[3], wide);
1349         }
1350     }

1351 }
< prev index next >