# Path: _stbt/utils.py
# def to_unicode(text):
#     if isinstance(text, bytes):
#         return text.decode("utf-8", errors="replace")
#     else:
#         return str(text)


import logging
import re
import sys
import threading
import time
            import cec  # pylint:disable=import-error
    import io
    import pytest
        import ctypes
from contextlib import contextmanager
from textwrap import dedent
from .utils import to_unicode
        from .control import UnknownKeyError
    from .control import uri_to_control
    from .control import uri_to_control
        from unittest.mock import patch
        from mock import patch  # Python 2 backport

        with self.lock:
            if not self.lib.Transmit(self.keyup_command()):
                raise HdmiCecError("Failed to send keyup for %s" % key)
        logging.debug("HdmiCecControl: Released %s", key)

    def send_keydowns(self, key):
        # CEC spec section 13.13.3 says that the receiver should assume a keyup
        # if it hasn't seen one within a receiver-determined timeframe which
        # can't be less than 550ms. For press-and-hold the initiator should
        # send repeated keydown commands (200 to 450ms apart) followed by the
        # final keyup.
        end_time = time.time() + 60
        next_press_deadline = time.time() + 0.3
        with self.lock:
            while True:
                self.lock.wait(max(0, next_press_deadline - time.time()))
                if not self.press_and_holding:
                    return
                if time.time() > end_time:
                    logging.warning("HdmiCecControl: Releasing %s as I've been "
                                    "holding it longer than 60 seconds", key)
                    return
                next_press_deadline = time.time() + 0.3
                if not self.lib.Transmit(self.keydown_command(key)):
                    logging.warning("HdmiCecControl: Failed to send repeated "
                                    "keydown for %s", key)

    def keydown_command(self, key):
        keycode = self.get_keycode(key)

# Path: _stbt/utils.py
# def to_unicode(text):
#     if isinstance(text, bytes):
#         return text.decode("utf-8", errors="replace")
#     else:
#         return str(text)


# Path: _stbt/control_gpl.py
import logging
import re
import sys
import threading
import time
            import cec  # pylint:disable=import-error
    import io
    import pytest
        import ctypes
from contextlib import contextmanager
from textwrap import dedent
from .utils import to_unicode
        from .control import UnknownKeyError
    from .control import uri_to_control
    from .control import uri_to_control
        from unittest.mock import patch
        from mock import patch  # Python 2 backport


        with self.lock:
            if not self.lib.Transmit(self.keyup_command()):
                raise HdmiCecError("Failed to send keyup for %s" % key)
        logging.debug("HdmiCecControl: Released %s", key)

    def send_keydowns(self, key):
        # CEC spec section 13.13.3 says that the receiver should assume a keyup
        # if it hasn't seen one within a receiver-determined timeframe which
        # can't be less than 550ms. For press-and-hold the initiator should
        # send repeated keydown commands (200 to 450ms apart) followed by the
        # final keyup.
        end_time = time.time() + 60
        next_press_deadline = time.time() + 0.3
        with self.lock:
            while True:
                self.lock.wait(max(0, next_press_deadline - time.time()))
                if not self.press_and_holding:
                    return
                if time.time() > end_time:
                    logging.warning("HdmiCecControl: Releasing %s as I've been "
                                    "holding it longer than 60 seconds", key)
                    return
                next_press_deadline = time.time() + 0.3
                if not self.lib.Transmit(self.keydown_command(key)):
                    logging.warning("HdmiCecControl: Failed to send repeated "
                                    "keydown for %s", key)

    def keydown_command(self, key):
        keycode = self.get_keycode(key)

        keydown_str = to_unicode("%X%X:44:%02X") % (