File size: 3,369 Bytes
4cb60dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Xlib.ext.ge -- Generic Event extension module
#
#    Copyright (C) 2012 Outpost Embedded, LLC
#      Forest Bond <forest.bond@rapidrollout.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2.1
# of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
#    Free Software Foundation, Inc.,
#    59 Temple Place,
#    Suite 330,
#    Boston, MA 02111-1307 USA

'''
ge - Generic Event Extension
'''

from Xlib.protocol import rq

extname = 'Generic Event Extension'


GenericEventCode = 35


class GEQueryVersion(rq.ReplyRequest):
    _request = rq.Struct(
        rq.Card8('opcode'),
        rq.Opcode(0),
        rq.RequestLength(),
        rq.Card32('major_version'),
        rq.Card32('minor_version'),
        )
    _reply = rq.Struct(
        rq.ReplyCode(),
        rq.Pad(1),
        rq.Card16('sequence_number'),
        rq.ReplyLength(),
        rq.Card32('major_version'),
        rq.Card32('minor_version'),
        rq.Pad(16),
        )


def query_version(self):
    return GEQueryVersion(
        display=self.display,
        opcode=self.display.get_extension_major(extname),
        major_version=1,
        minor_version=0,
        )


class GenericEvent(rq.Event):
    _code = GenericEventCode
    _fields = rq.Struct(
        rq.Card8('type'),
        rq.Card8('extension'),
        rq.Card16('sequence_number'),
        rq.Card32('length'),
        rq.Card16('evtype'),
        # Some generic events make use of this space, but with
        # others the data is simply discarded.  In any case we
        # don't need to explicitly pad this out as we are
        # always given at least 32 bytes and we save
        # everything after the first ten as the "data" field.
        #rq.Pad(22),
        )

    def __init__(self, binarydata = None, display = None, **keys):
        if binarydata:
            data = binarydata[10:]
            binarydata = binarydata[:10]
        else:
            data = ''

        rq.Event.__init__(
            self,
            binarydata=binarydata,
            display=display,
            **keys
            )

        if display:
            ge_event_data = getattr(display, 'ge_event_data', None)
            if ge_event_data:
                estruct = ge_event_data.get((self.extension, self.evtype), None)
                if estruct:
                    data, _ = estruct.parse_binary(data, display)

        self._data['data'] = data


def add_event_data(self, extension, evtype, estruct):
    if not hasattr(self.display, 'ge_event_data'):
        self.display.ge_event_data = {}
    self.display.ge_event_data[(extension, evtype)] = estruct


def init(disp, info):
    disp.extension_add_method('display', 'ge_query_version', query_version)
    disp.extension_add_method('display', 'ge_add_event_data', add_event_data)
    disp.extension_add_event(GenericEventCode, GenericEvent)