Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate Requests #3

Open
wants to merge 43 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
6657b88
adding xml/json spec as sub-project
CrypticSwarm Sep 5, 2011
49ae09c
moving/modifing pack value mask helper to seperate file.
CrypticSwarm Sep 5, 2011
d8b3b8a
quick mockup of generating x requests.
CrypticSwarm Sep 5, 2011
e7a4e89
updates info for valuemasks.
CrypticSwarm Sep 5, 2011
65c74b1
fix a few errors and calulate size of request.
CrypticSwarm Sep 5, 2011
804be9a
convert a test to use the new request format.
CrypticSwarm Sep 5, 2011
7d3b211
update request generator to give simple responses.
CrypticSwarm Sep 5, 2011
ab3ad6e
show QueryTree response.
CrypticSwarm Sep 5, 2011
8597bd8
added char list packing / unpacking.
CrypticSwarm Sep 7, 2011
ff58ea5
changed atoms test.
CrypticSwarm Sep 7, 2011
bd20afb
make hidden props non enumerable.
CrypticSwarm Sep 7, 2011
db524a7
rearranging and properly indenting generated code.
CrypticSwarm Sep 7, 2011
5d8ae42
move some util functions out of the auto generated file.
CrypticSwarm Sep 8, 2011
ebbbbcc
makes the offset added to result of buf.unpack array non enumerable
CrypticSwarm Sep 8, 2011
e6da58a
update protocols sub project
CrypticSwarm Sep 8, 2011
cd28911
added a pack/unpack struct template file
CrypticSwarm Sep 8, 2011
545ca43
fleshed out pack and unpack sub templates
CrypticSwarm Sep 8, 2011
cc78999
generated the requests and structs with new templates.
CrypticSwarm Sep 8, 2011
37ff1d0
added a ListHosts test/example
CrypticSwarm Sep 8, 2011
d529073
requests requiring lists the fields are now added appropriately.
CrypticSwarm Sep 9, 2011
bd97623
added a rectangles test.
CrypticSwarm Sep 9, 2011
fae0e8b
removing unneeded rectangle_len in test/rect
CrypticSwarm Sep 9, 2011
4f1a8a7
fixes adding length specifier when shouldn't.
CrypticSwarm Sep 9, 2011
225e124
allows lists of items to be either a single or array of items.
CrypticSwarm Sep 9, 2011
6d5b414
updated alloccolor test
CrypticSwarm Sep 9, 2011
5478a6a
fixes problem with void data incorrectly packed/unpacked.
CrypticSwarm Sep 9, 2011
edc1fc6
brings test/changeproperty to the new api
CrypticSwarm Sep 9, 2011
d7830e3
brings test/creategc to new api
CrypticSwarm Sep 9, 2011
eaaa2f9
brings test/createwindow to new api
CrypticSwarm Sep 9, 2011
3b70fcf
brings test/genstdatoms to new api
CrypticSwarm Sep 9, 2011
8cd04d3
cleanup delete unused c-test files
CrypticSwarm Sep 9, 2011
00fc7c3
fixes issues that GetKeyboardMapping had
CrypticSwarm Sep 9, 2011
9a84a4d
brings test/getkeyboardmapping to new api
CrypticSwarm Sep 9, 2011
6fa0d51
fixes reply lists with operators and body length usage.
CrypticSwarm Sep 9, 2011
08b6d12
brings test/getprop to new api
CrypticSwarm Sep 9, 2011
a8432ff
fixes prop not being added for unpacking toa struct in some situations
CrypticSwarm Sep 9, 2011
b007587
makes Buffer.offset writeable.
CrypticSwarm Sep 9, 2011
437f9b1
brings test/testext to new api
CrypticSwarm Sep 9, 2011
5edff65
brings test/polyfillrect to new api
CrypticSwarm Sep 9, 2011
abaade7
brings test/polyline to new api
CrypticSwarm Sep 9, 2011
005095d
brings test/polyline to new api
CrypticSwarm Sep 9, 2011
c505296
brings test/query_ext to new api
CrypticSwarm Sep 9, 2011
f5290b6
brings test/query_pointer to new api
CrypticSwarm Sep 9, 2011
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "protocols"]
path = protocols
url = git://github.com/CrypticSwarm/XProto2JSON.git
211 changes: 211 additions & 0 deletions generateFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
var fs = require('fs')
, jqtpl = require("jqtpl")
, typeLookup = JSON.parse(fs.readFileSync('protocols/xTypes.json'))
, xProto = JSON.parse(fs.readFileSync('protocols/xProtocol.json'))
, data = { requests: xProto.request
, structs: xProto.struct
, getValueMask: getValueMask
, getDelim: getDelim
, enumVal: enumVal
, getBufPack: getBufPack
, isListType: isListType
, prepPropName: prepPropName
, firstType: firstType
, shiftedFirstType: shiftedFirstType
, prePackFirst: prePackFirst
, isValueMask: isValueMask
, isListAccountedFor: isListAccountedFor
, bufPackType: bufPackType
, requestLengthIndex: requestLengthIndex
, listLenIndex: listLenIndex
, realIndex: realIndex
, unpackList: unpackList
, packList: packList
, getFieldRef: getFieldRef
, isFieldFirst: isFieldFirst
, listLengthName: listLengthName
, fieldName: fieldName
, nonPad: nonPad
, constructOp: constructOp
}

;['requests', 'structs'].forEach(function(name) {
var tplFile = fs.readFileSync('stubs/' + name + '.tpl').toString().replace(/\n\s*{{/g, '{{').replace(/}}\s*$/g, '}}')
fs.writeFileSync('lib/x11/autogen/' + name + '.js', jqtpl.tmpl(tplFile, data))
})

function getValueMask(requestName) {
return typeLookup.valuemaskEnum[requestName] && xProto.enum[typeLookup.valuemaskEnum[requestName]].field
}

function getDelim(i, first, rest) {
return i == 0 ? (first != null ? first : '{') : (rest != null ? rest : ',')
}

function enumVal(val) {
return val.value ? val.value
: val.bit ? 1 << val.bit
: 0
}

function isListType(field) {
return field.fieldType === 'list' || field.fieldType === 'valueparam'
}

function getBufPack(field) {
return !field ? 'x'
: field.fieldType === 'pad' ? new Array(parseInt(field.bytes) + 1).join('x')
: isListType(field) ? ''
: typeLookup.packTypes[field.type]
}

function bufPackType(type) {
return typeLookup.packTypes[type]
}

function prepPropName(short) {
return short === 'new' ? '_new'
: short === 'class' ? '_class'
: short === 'delete' ? '_delete'
: short
}

function firstType(field, type) {
var f
return field.some(function(field) {
f = field
return field.fieldType == type
}) && f
}

function shiftedFirstType(field, type) {
return firstType(field.slice(1), 'field')
}

function prePackFirst(request) {
return request.opcode < 128
}

function requestLengthIndex(request) {
return prePackFirst(request) && request.field && request.field[0].fieldType != 'pad' ? 2 : 1
}

function isValueMask(field) {
return field.fieldType == 'valueparam'
}

function listLenName(field) {
return field.fieldType == 'list' ? field.name + '_len'
: field['value-mask-name']
}

function isListAccountedFor(request, field) {
var name = listLenName(field)
return field.name == 'keysyms'
|| field.name == 'keycodes'
|| field.name == 'event'
|| !request.field.every(function(f) {
return name != f.name
})
}

function listLenIndex(request, field) {
var index = 1
, name = listLenName(field)
for (var i = 0, len = request.field.length; i < len; ++i) {
if (request.field[i].name == name) return index
if (request.field[i].fieldType == 'field') index++;
}
}

function realIndex(request, field) {
var index = 0
for (var i = 0, len = request.length; i < len; ++i) {
if (request[i] == field) return index
if (request[i].fieldType == 'field') index++;
}
}

function unpackList(indents, obj, buf, offset, list, name) {
var subData = {} //Object.create(data)
, key
for (key in data) subData[key] = data[key]
subData.buf = buf
subData.offset = offset
subData.obj = obj
subData.list = list
subData.theName = name
var tplFile = fs.readFileSync('stubs/unpackList.tpl').toString().replace(/\n\s*{{/g, '{{').replace(/}}\s*$/g, '}}')
return indent(indents, jqtpl.tmpl(tplFile, subData))
}

function packList(indents, args, format, list, name) {
var subData = {} //Object.create(data)
, key
for (key in data) subData[key] = data[key]
subData.args = args
subData.format = format
subData.list = list
subData.theName = name
var tplFile = fs.readFileSync('stubs/packList.tpl').toString().replace(/\n\s*{{/g, '{{').replace(/}}\s*$/g, '}}')
return indent(indents, jqtpl.tmpl(tplFile, subData))
}

function indent(num, lines) {
var indent = new Array(num + 1).join(' ')
return lines.split('\n').map(function(line) {
return line ? indent + line : ''
}).join('\n')
}

function getFieldRef(name, field, obj) { //todo read operators appropriately
return field.fieldType == 'valueparam' ? field['value-mask-name']
: field.fieldref ? field.fieldref
: field.value ? field.value
: name == 'GetProperty' ? obj + ".format / 8 * " + obj + ".value_len"
: name == 'GetModifierMapping' ? obj + ".keycodes_per_modifier * 8"
: "0"
}

function listLengthName(field) {
return field.fieldType == 'valueparam' ? field['value-mask-name']
: field.fieldref ? field.fieldref
: field.value ? field.value
: field.name + '_len'
}

function isFieldFirst(fields) {
return fields[0].fieldType == 'field'
}

function fieldName(field) {
return prepPropName(
field.fieldType == 'valueparam' ? field['value-list-name']
: field.name
)
}

function nonPad(fields){
return fields && fields.filter(function(field){
return field.fieldType != 'pad'
})
}

function constructOp(field, obj) {
function prependObj(item) {
return item == 'length' ? item : obj + '.' + item
}

function handleOp(op) {
var params = []
if (op.value) params.push(op.value)
if (op.fieldref) {
if (Array.isArray(op.fieldref)) params.concat(op.fieldref.map(prependObj))
else params.push(prependObj(op.fieldref))
}
if (op.op) params.push(handleOp(op.op))
return params.join(op.operation)
}

return handleOp(field.op)
}
Loading