File indexing completed on 2024-10-13 13:10:03

0001 # Copyright 2021 Aditya Mehra <aix.m@outlook.com>.
0002 #
0003 # Licensed under the Apache License, Version 2.0 (the "License");
0004 # you may not use this file except in compliance with the License.
0005 # You may obtain a copy of the License at
0006 #
0007 #    http://www.apache.org/licenses/LICENSE-2.0
0008 #
0009 # Unless required by applicable law or agreed to in writing, software
0010 # distributed under the License is distributed on an "AS IS" BASIS,
0011 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 # See the License for the specific language governing permissions and
0013 # limitations under the License.
0014 #
0015 
0016 import sys
0017 import libinput
0018 from mycroft_bus_client import MessageBusClient, Message
0019 
0020 class MycroftPTT:
0021 
0022     def __init__(self):
0023         # init messagebus client
0024         client = MessageBusClient()
0025         client.run_in_thread()
0026         client.on('recognizer_loop:record_begin', self.set_mic_active)
0027         client.on('recognizer_loop:record_end', self.set_mic_inactive)
0028         self.mic_active = False
0029 
0030         # init libinput
0031         li = libinput.LibInput(udev=True)
0032         li.udev_assign_seat('seat0')
0033 
0034         # loop which reads events
0035         for event in li.get_event():
0036             # test the event.type to filter out only keyboard events
0037             if event.type == libinput.constant.Event.KEYBOARD_KEY:
0038                 # get the details of the keyboard event
0039                 kbev = event.get_keyboard_event()
0040                 kcode = kbev.get_key() # constants in libinput.define.Key.KEY_xxx
0041                 kstate = kbev.get_key_state() # constants libinput.constant.KeyState.PRESSED or .RELEASED
0042                 ktype = libinput.define.Key
0043 
0044                 # your key handling will look something like this...
0045                 if kstate == libinput.constant.KeyState.PRESSED:
0046                     if kbev.get_key() == ktype.KEY_VOICECOMMAND or kbev.get_key() == ktype.KEY_ASSISTANT:
0047                         if not self.mic_active:
0048                             client.emit(Message('mycroft.mic.listen'))
0049 
0050                 elif kstate == libinput.constant.KeyState.RELEASED:
0051                     if kbev.get_key() == ktype.KEY_VOICECOMMAND or kbev.get_key() == ktype.KEY_ASSISTANT:
0052                         print("Key Voice Command released")
0053 
0054     def set_mic_active(self, _):
0055         self.mic_active = True
0056 
0057     def set_mic_inactive(self, _):
0058         self.mic_active = False
0059 
0060 def main():
0061     daemon = MycroftPTT()
0062 
0063 if __name__ == "__main__":
0064     main()