py: drop the fancy event handling
[android/external-libkmsxx.git] / py / pykms / __init__.py
1 from .pykms import *
2 from enum import Enum
3 import os
4 import struct
6 #
7 # Common RGB colours
8 #
10 red = RGB(255, 0, 0)
11 green = RGB(0, 255, 0)
12 blue = RGB(0, 0, 255)
13 yellow = RGB(255, 255, 0)
14 purple = RGB(255, 0, 255)
15 white = RGB(255, 255, 255)
16 cyan = RGB(0, 255, 255)
18 #
19 # Rotation enum
20 #
22 class Rotation(int, Enum):
23     ROTATE_0 = 1 << 0
24     ROTATE_90 = 1 << 1
25     ROTATE_180 = 1 << 2
26     ROTATE_270 = 1 << 3
27     ROTATE_MASK = ROTATE_0 | ROTATE_90 | ROTATE_180 | ROTATE_270
28     REFLECT_X = 1 << 4
29     REFLECT_Y = 1 << 5
30     REFLECT_MASK = REFLECT_X | REFLECT_Y
32 #
33 # DrmObject API extensions
34 #
36 def __obj_set_prop(self, prop, value):
37     if self.card.has_atomic:
38         areq = AtomicReq(self.card)
39         areq.add(self, prop, value)
40         if areq.commit_sync() != 0:
41             print("commit failed")
42     else:
43         if self.set_prop_value(prop, value) != 0:
44             print("setting property failed")
46 def __obj_set_props(self, map):
47     if self.card.has_atomic:
48         areq = AtomicReq(self.card)
50         for key, value in map.items():
51             areq.add(self, key, value)
53         if areq.commit_sync() != 0:
54             print("commit failed")
55     else:
56         for propid,propval in map.items():
57             if self.set_prop_value(propid, propval) != 0:
58                 print("setting property failed")
60 DrmObject.set_prop = __obj_set_prop
61 DrmObject.set_props = __obj_set_props
63 #
64 # Card API extensions
65 #
67 def __card_disable_planes(self):
68     areq = AtomicReq(self)
70     for p in self.planes:
71         areq.add(p, "FB_ID", 0)
72         areq.add(p, "CRTC_ID", 0)
74     if areq.commit_sync() != 0:
75         print("disabling planes failed")
77 Card.disable_planes = __card_disable_planes
79 class DrmEventType(Enum):
80     VBLANK = 0x01
81     FLIP_COMPLETE = 0x02
83 # struct drm_event {
84 #   __u32 type;
85 #   __u32 length;
86 #};
87 #
89 _drm_ev = struct.Struct("II")
91 #struct drm_event_vblank {
92 #   struct drm_event base;
93 #   __u64 user_data;
94 #   __u32 tv_sec;
95 #   __u32 tv_usec;
96 #   __u32 sequence;
97 #   __u32 reserved;
98 #};
100 _drm_ev_vbl = struct.Struct("QIIII") # Note: doesn't contain drm_event
102 class DrmEvent:
103     def __init__(self, type, seq, time, data):
104         self.type = type
105         self.seq = seq
106         self.time = time
107         self.data = data
109 # Return DrmEvents. Note: blocks if there's nothing to read
110 def __card_read_events(self):
111     buf = os.read(self.fd, _drm_ev_vbl.size * 20)
113     if len(buf) == 0:
114         return
116     if len(buf) < _drm_ev.size:
117         raise RuntimeError("Partial DRM event")
119     idx = 0
121     while idx < len(buf):
122         ev_tuple = _drm_ev.unpack_from(buf, idx)
124         type = DrmEventType(ev_tuple[0])
126         if type != DrmEventType.VBLANK and type != DrmEventType.FLIP_COMPLETE:
127             raise RuntimeError("Illegal DRM event type")
129         vbl_tuple = _drm_ev_vbl.unpack_from(buf, idx + _drm_ev.size)
131         seq = vbl_tuple[3]
132         time = vbl_tuple[1] + vbl_tuple[2] / 1000000.0;
133         udata = vbl_tuple[0]
135         yield DrmEvent(type, seq, time, udata)
137         idx += ev_tuple[1]
139 Card.read_events = __card_read_events