-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerateFiles.js
More file actions
196 lines (165 loc) · 5.35 KB
/
generateFiles.js
File metadata and controls
196 lines (165 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var fs = require('fs')
, jqtpl = require("jqtpl")
, typeLookup = JSON.parse(fs.readFileSync('protocols/xTypes.json'))
, xProto = JSON.parse(fs.readFileSync('protocols/xProtocol.json'))
, jsTypes = typeLookup.xtojs
, cTypes = typeLookup.xtoc
, data =
{ prepPropName: prepPropName
, xcbEventType: xcbEventType
, xcbEnumName: xcbEnumName
, JSType: JSType
, shortEventName: shortEventName
, getPrepName: getPrepName
, getSymName: getSymName
, XCBType: getXCBType
, getJSTypeExample: getJSTypeExample
, getDocHelp: getDocHelp
, DocName: DocName
, enumValue: enumValue
, getXCBReqName: getXCBReqName
, isListType: isListType
, includeListLen: includeListLen
, listLenName: listLenName
, listLenType: listLenType
, getParamList: getParamList
, listJSType: listJSType
, listType: listType
, listName: listName
, tempListHolder: tempListHolder
, XCBReplyType: XCBReplyType
, XCBCookieType: XCBCookieType
, XCBReplyFunction: XCBReplyFunction
, isLenSpecifier: isLenSpecifier
, events: xProto.event
, structs: xProto.struct
, requests: xProto.request
, enums: xProto.enum
}
Object.keys(xProto.eventcopy).forEach(function(x) {
data.events[x] = xProto.eventcopy[x]
})
;['events', 'structs', 'enum', 'requests'].forEach(function(name) {
var tplFile = fs.readFileSync('stubs/' + name + '.tpl').toString().replace(/\n\s*{{/g, '{{').replace(/}}\s*$/g, '}}')
fs.writeFileSync('src/__autogen_' + name + '.h', jqtpl.tmpl(tplFile, data))
})
function getSymName(short) {
return 'on' + short + "Sym"
}
function getPrepName(short) {
if (!data.events[short].field && data.events[short].ref) short = data.events[short].ref
return "prepare" + short + "Event"
}
function prepPropName(short) {
return short === 'new' ? '_new'
: short === 'class' ? '_class'
: short === 'delete' ? '_delete'
: short
}
function shortEventName(name) {
return name.replace('Notify', '')
}
function xcbEventType(eventName) {
return xcbEnumName(eventName).toLowerCase() + "_event_t"
}
function xcbEnumName(eventName) {
return ("xcb" + eventName).replace(/([A-Z])/g, function(letter) {
return '_' + letter
}).toUpperCase()
}
function JSType(short) {
var type = short == 'TIMESTAMP' ? 'Integer': jsTypes[short]
if (!type && !data.structs[short]) return console.log("Couldn't find the appropriate V8 type for", short, "for to JS generation:")
return type
}
function getXCBType(short) {
return cTypes[short] ? cTypes[short] : ('xcb' + short + '_t') .replace(/[a-z][A-Z]/g, function(rep) {
return rep[0] + '_' + rep[1]
}).toLowerCase()
}
function DocName(short) {
return getXCBType(short).replace(/xcb_|_t/g, '').replace(/_[a-z]/g, function(m) { return m[1].toUpperCase() })
}
function getDocHelp(short, section) {
if (!section[short].field && section[short].ref) short = section[short].ref
if (!section[short].field) return '{ }'
return '{ ' + section[short].field.filter(function(field) {
return field.fieldType == 'field'
}).map(function(field) {
return field.name + ": " + getJSTypeExample(JSType(field.type))
}).join('\\n, ') + ' }'
}
function getJSTypeExample(type) {
return type
}
function enumValue(enumItem){
return enumItem.value ? enumItem.value : 1 << parseInt(enumItem.bit)
}
function getXCBReqName(short) {
return ('xcb' + short) .replace(/[a-z][A-Z0-9]/g, function(rep) {
return rep[0] + '_' + rep[1]
}).toLowerCase()
}
function isListType(field){
return field.fieldType == 'list' || field.fieldType == 'valueparam'
}
function tempListHolder(field) {
return field.type == 'char' ? field.name + '_str'
: field.fieldType == 'list' ? field.name + '_list'
: field.fieldType == 'valueparam' ? field['value-mask-name'] + '_valmask'
: ''
}
function listName(field) {
return field.fieldType == 'list' ? field.name
: field['value-list-name']
}
function listType(field) {
return field.fieldType == 'valueparam' ? 'uint32_t'
: getXCBType(field.type)
}
function listJSType(field) {
return field.fieldType == 'valueparam' ? 'Integer'
: JSType(field.type)
}
function getParamList(fields) {
var str = fields && fields.filter(function (field){
return field.fieldType == 'field' || isListType(field)
}).map(function(field) {
return !isListType(field) ? prepPropName(field.name)
: includeListLen(field, fields) ? listLenName(field) + ", " + listName(field)
: listName(field)
}).join(', ')
return str ? ', ' + str : ''
}
function listLenType(field) {
return field.fieldType == 'list' ? 'uint32_t'
: getXCBType(field['value-mask-type'])
}
function listLenName(field) {
return field.fieldType == 'list' ? field.name + '_len'
: field['value-mask-name']
}
function includeListLen(field, fields){
var name = listLenName(field)
return field.name != 'keysyms'
&& field.name != 'keycodes'
&& field.name != 'event'
&& fields.every(function(f) {
return name != f.name
})
}
function camelToUnder(name) {
return name.replace(/([A-Z])/g, '_$1').toLowerCase()
}
function XCBReplyType(requestName) {
return 'xcb' + camelToUnder(requestName) + '_reply_t'
}
function XCBCookieType(requestName) {
return 'xcb' + camelToUnder(requestName) + '_cookie_t'
}
function XCBReplyFunction(requestName) {
return 'xcb' + camelToUnder(requestName) + '_reply'
}
function isLenSpecifier(field) {
return /_len$/.test(field.name)
}