Skip to content

Commit

Permalink
Adding support for generic events
Browse files Browse the repository at this point in the history
  • Loading branch information
moaxcp committed Apr 16, 2021
1 parent 31a54cb commit d7cff50
Show file tree
Hide file tree
Showing 13 changed files with 445 additions and 96 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,11 @@ and will likely move into a new project.

# versions

## 0.7.0-SNAPSHOT

* Adding support for generic events
* Added ProtocolPluginService to handle plugins for the XProtocolService

## 0.6.0

* Implementing `sync()` method based on XSync but without a discard parameter.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ x11Protocol {
outputSrc = file('build/generated/xproto/java/')
outputResources = file('build/generated/xproto/resources')
xcbXmls = file('src/main/xcbXmls')
exclude = ['present', 'xinput', 'xkb', 'dri3', 'glx']
exclude = ['xinput', 'xkb', 'dri3', 'glx']
keysymHeader = file('src/main/keysym/keysymdef.h')
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,9 @@ class ClientMessageDataUnion extends JavaUnion {
MethodSpec getWriteMethod() {
return null
}

@Override
Optional<Integer> getFixedSize() {
return Optional.of(20)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ import static com.github.moaxcp.x11protocol.generator.Conventions.getEventTypeNa

class JavaEvent extends JavaObjectType {
int number
boolean genericEvent
int genericEventNumber

static JavaEvent javaEvent(XTypeEvent event) {
String simpleName = getEventJavaName(event.name)

ClassName superType = ClassName.get(event.basePackage, 'XEvent')
if(event.genericEvent) {
superType = ClassName.get(event.basePackage, 'XGenericEvent')
}
JavaEvent javaEvent = new JavaEvent(
superTypes: event.superTypes + ClassName.get(event.basePackage, 'XEvent'),
superTypes: event.superTypes + superType,
basePackage: event.basePackage,
javaPackage: event.javaPackage,
simpleName:simpleName,
Expand All @@ -27,6 +32,20 @@ class JavaEvent extends JavaObjectType {
if(javaEvent.fixedSize && javaEvent.fixedSize.get() < 32) {
javaEvent.protocol.add(new JavaPad(bytes: 32 - javaEvent.fixedSize.get()))
}

if(event.genericEvent) {
javaEvent.genericEvent = true
javaEvent.genericEventNumber = event.genericEventNumber
JavaProperty l = javaEvent.getJavaProperty('length')
l.writeValueExpression = CodeBlock.of('getLength() - 32')
if(!(javaEvent.protocol[1] instanceof JavaReadParameter)) {
throw new IllegalStateException("First field must be a JavaReadParameter")
}
javaEvent.getJavaProperty('extension').readParam = true
javaEvent.getJavaProperty('sequenceNumber').readParam = true
javaEvent.getJavaProperty('length').readParam = true
javaEvent.getJavaProperty('eventType').readParam = true
}
return javaEvent
}

Expand All @@ -51,30 +70,59 @@ class JavaEvent extends JavaObjectType {

@Override
void addReadParameters(MethodSpec.Builder methodBuilder) {
super.addReadParameters(methodBuilder)
methodBuilder.addParameter(ParameterSpec.builder(TypeName.BOOLEAN, 'sentEvent').build())
super.addReadParameters(methodBuilder)
}

@Override
void addWriteStatements(MethodSpec.Builder methodBuilder) {
super.addWriteStatements(methodBuilder)
if(fixedSize && fixedSize.get() >= 32) {
return
void addReadStatements(MethodSpec.Builder methodBuilder) {
if(lastListNoLength) {
if(genericEvent) {
methodBuilder.addStatement('int javaStart = 10')
} else {
methodBuilder.addStatement('int javaStart = 1')
}
protocol.eachWithIndex { it, i ->
if(!it.readProtocol
|| (it instanceof JavaProperty && it.bitcaseInfo)) {
return
}
methodBuilder.addCode(it.declareAndReadCode)
if(i != protocol.size() - 1) {
methodBuilder.addStatement('javaStart += $L', it.getSizeExpression())
}
}
} else {
super.addReadStatements(methodBuilder)
}
if(fixedSize && fixedSize.get() < 32) {
methodBuilder.addStatement('in.readPad($L)', 32 - fixedSize.get())
}
methodBuilder.beginControlFlow('if(getSize() < 32)')
.addStatement('out.writePad(32 - getSize())')
.endControlFlow()
}

@Override
void addBuilderStatement(MethodSpec.Builder methodBuilder, CodeBlock... fields) {
CodeBlock sentEvent = CodeBlock.builder().addStatement('javaBuilder.$L($L)', 'sentEvent', 'sentEvent').build()
super.addBuilderStatement(methodBuilder, sentEvent)
if(fixedSize && fixedSize.get() >= 32) {
CodeBlock.Builder startBuilder = CodeBlock.builder().addStatement('javaBuilder.$L($L)', 'sentEvent', 'sentEvent')
super.addBuilderStatement(methodBuilder, startBuilder.build())
if(!fixedSize) {
methodBuilder.beginControlFlow('if(javaBuilder.getSize() < 32)')
methodBuilder.addStatement('in.readPad(32 - javaBuilder.getSize())')
methodBuilder.endControlFlow()
return
}

if(fixedSize && fixedSize.get() % 4 == 0) {
return
}
methodBuilder.addStatement('in.readPadAlign(javaBuilder.getSize())')
}

@Override
void addWriteStatements(MethodSpec.Builder methodBuilder) {
super.addWriteStatements(methodBuilder)
if(fixedSize && fixedSize.get() % 4 == 0) {
return
}
methodBuilder.beginControlFlow('if(javaBuilder.getSize() < 32)')
.addStatement('in.readPad(32 - javaBuilder.getSize())')
.endControlFlow()
methodBuilder.addStatement('out.writePadAlign(getSize())')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ class JavaTypeListProperty extends JavaListProperty {
@Override
CodeBlock getDeclareAndReadCode() {
if(lengthExpression instanceof EmptyExpression) {
JavaProperty lengthProperty = javaType.getJavaProperty('length')
CodeBlock lengthExpression = CodeBlock.of('$L', lengthProperty.name)
if(lengthProperty.typeName == TypeName.SHORT) {
lengthExpression = CodeBlock.of('Short.toUnsignedInt($L)', lengthProperty.name)
}
return CodeBlock.builder()
.addStatement('$1T $2L = new $3T<>(length - javaStart)', typeName, name, ArrayList.class)
.beginControlFlow('while(javaStart < Short.toUnsignedInt(length) * 4)')
.beginControlFlow('while(javaStart < $L * 4)', lengthExpression)
.addStatement('$1T baseObject = $1T.read$2L(in)', baseTypeName, baseTypeName.simpleName())
.addStatement('$L.add(baseObject)', name)
.addStatement('javaStart += baseObject.getSize()')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,17 @@ class XResult {
.build())

builder.addField(
FieldSpec.builder(byte.class, 'majorOpcode', Modifier.PRIVATE)
FieldSpec.builder(byte.class, 'majorVersion', Modifier.PRIVATE)
.addAnnotation(Getter.class)
.addAnnotation(Setter.class)
.initializer('$L', majorVersion)
.build())

builder.addField(
FieldSpec.builder(byte.class, 'minorVersion', Modifier.PRIVATE)
.addAnnotation(Getter.class)
.initializer('$L', minorVersion)
.build())

builder.addField(
FieldSpec.builder(byte.class, 'firstEvent', Modifier.PRIVATE)
.addAnnotation(Getter.class)
Expand Down Expand Up @@ -96,9 +102,11 @@ class XResult {
.addParameter(byte.class, 'number')

for(XTypeEvent event : events.values()) {
supportedEvent.beginControlFlow('if(number + firstEvent == $L)', event.number)
supportedEvent.addStatement('return true')
supportedEvent.endControlFlow()
if(event.number != 35) {
supportedEvent.beginControlFlow('if(number + firstEvent == $L)', event.number)
supportedEvent.addStatement('return true')
supportedEvent.endControlFlow()
}
}
supportedEvent.addStatement('return false')

Expand Down Expand Up @@ -129,9 +137,11 @@ class XResult {
.addException(IOException.class)

for(XTypeEvent event : events.values()) {
readEvent.beginControlFlow('if(number + firstEvent == $L)', event.number)
readEvent.addStatement('return $T.read$L(sentEvent, in)', event.javaType.className, event.javaType.className.simpleName())
readEvent.endControlFlow()
if(event.number != 35) {
readEvent.beginControlFlow('if(number + firstEvent == $L)', event.number)
readEvent.addStatement('return $T.read$L(sentEvent, in)', event.javaType.className, event.javaType.className.simpleName())
readEvent.endControlFlow()
}
}
readEvent.addStatement('throw new $T("number " + number + " is not supported")', IllegalArgumentException.class)

Expand All @@ -154,10 +164,33 @@ class XResult {

builder.addMethod(readError.build())

MethodSpec.Builder readGenericEvent = MethodSpec.methodBuilder('readGenericEvent')
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.returns(ClassName.get(basePackage, 'XGenericEvent'))
.addParameter(boolean.class, 'sentEvent')
.addParameter(byte.class, 'extension')
.addParameter(short.class, 'sequenceNumber')
.addParameter(int.class, 'length')
.addParameter(short.class, 'eventType')
.addParameter(ClassName.get(basePackage, 'X11Input'), 'in')
.addException(IOException.class)

for(XTypeEvent event : events.values()) {
if(event.number == 35) {
readGenericEvent.beginControlFlow('if(eventType == $L)', event.genericEventNumber)
readError.addStatement('return $T.read$L(sentEvent, extension, sequenceNumber, length, eventType, in)', event.javaType.className, event.javaType.className.simpleName())
readGenericEvent.endControlFlow()
}
}
readGenericEvent.addStatement('throw new $T("eventType " + eventType + " is not supported")', IllegalArgumentException.class)

builder.addMethod(readGenericEvent.build())

return builder.build()
}

public ClassName getPluginClassName() {
ClassName getPluginClassName() {
ClassName.get(javaPackage, getJavaName(header.capitalize() + 'Plugin'))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,32 @@ import static com.github.moaxcp.x11protocol.xcbparser.JavaEvent.javaEvent

class XTypeEvent extends XTypeObject {
int number
boolean genericEvent
int genericEventNumber

XTypeEvent(Map map) {
super(map)
number = map.number ?: 0
genericEvent = map.genericEvent ?: false
}

static XTypeEvent xTypeEvent(XResult result, Node node) {
int number = Integer.valueOf((String) node.attributes().get('number'))
XTypeEvent event = new XTypeEvent(result: result, number: number, name: node.attributes().get('name'), basePackage: result.basePackage, javaPackage: result.javaPackage)
XTypeEvent event = new XTypeEvent(result: result, number: number, name: node.attributes().get('name'), genericEvent: node.attributes().get('xge'), basePackage: result.basePackage, javaPackage: result.javaPackage)
event.addUnits(result, node)
event.protocol.add(0, new XUnitField(result: result, name: 'NUMBER', type: 'CARD8', constantValue: number))
event.protocol.add(2, new XUnitField(result: result, name:'sequence_number', type: 'CARD16'))
if(event.genericEvent) {
event.number = 35
event.genericEventNumber = number
event.protocol.add(0, new XUnitField(result: result, name: 'NUMBER', type: 'CARD8', constantValue: 35))
event.protocol.add(1, new XUnitField(result: result, name: 'extension', type: 'CARD8'))
event.protocol.add(2, new XUnitField(result: result, name: 'sequence_number', type: 'CARD16'))
event.protocol.add(3, new XUnitField(result: result, name: 'length', type: 'CARD32', localOnly: true))
event.protocol.add(4, new XUnitField(result: result, name: 'event_type', type: 'CARD16', constantValue: number))

} else {
event.protocol.add(0, new XUnitField(result: result, name: 'NUMBER', type: 'CARD8', constantValue: number))
event.protocol.add(2, new XUnitField(result: result, name: 'sequence_number', type: 'CARD16'))
}

return event
}
Expand Down
Loading

0 comments on commit d7cff50

Please sign in to comment.