diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 18:07:22 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 18:07:22 +0000 |
commit | c04dcc2e7d834218ef2d4194331e383402495ae1 (patch) | |
tree | 7333e38d10d75386e60f336b80c2443c1166031d /tools/EventClients/examples/python | |
parent | Initial commit. (diff) | |
download | kodi-c04dcc2e7d834218ef2d4194331e383402495ae1.tar.xz kodi-c04dcc2e7d834218ef2d4194331e383402495ae1.zip |
Adding upstream version 2:20.4+dfsg.upstream/2%20.4+dfsg
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/EventClients/examples/python')
6 files changed, 390 insertions, 0 deletions
diff --git a/tools/EventClients/examples/python/example_action.py b/tools/EventClients/examples/python/example_action.py new file mode 100755 index 0000000..b87fc29 --- /dev/null +++ b/tools/EventClients/examples/python/example_action.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +# This is a simple example showing how you can send a key press event +# to XBMC using the XBMCClient class + +import os +from socket import * +import sys + +if os.path.exists("../../lib/python"): + # try loading modules from source directory + sys.path.append("../../lib/python") + + from xbmcclient import * + + ICON_PATH = "../../icons/" +else: + # fallback to system wide modules + + from kodi.xbmcclient import * + from kodi.defs import * + +def main(): + + host = "localhost" + port = 9777 + + # Create an XBMCClient object and connect + xbmc = XBMCClient("Example Remote", ICON_PATH + "/bluetooth.png") + xbmc.connect() + + # send a up key press using the xbox gamepad map "XG" and button + # name "dpadup" ( see PacketBUTTON doc for more details) + try: + xbmc.send_action(sys.argv[2], ACTION_BUTTON) + except: + try: + xbmc.send_action(sys.argv[1], ACTION_EXECBUILTIN) + except Exception as e: + print(str(e)) + xbmc.send_action("ActivateWindow(ShutdownMenu)") + + + # ok we're done, close the connection + # Note that closing the connection clears any repeat key that is + # active. So in this example, the actual release button event above + # need not have been sent. + xbmc.close() + +if __name__=="__main__": + main() diff --git a/tools/EventClients/examples/python/example_button1.py b/tools/EventClients/examples/python/example_button1.py new file mode 100755 index 0000000..abdfe49 --- /dev/null +++ b/tools/EventClients/examples/python/example_button1.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python3 + +# This is a simple example showing how you can send 2 button events +# to XBMC in a queued fashion to shut it down. + +# Queued button events are not repeatable. + +# The basic idea is to create single packets and shoot them to XBMC +# The provided library implements some of the support commands and +# takes care of creating the actual packet. Using it is as simple +# as creating an object with the required constructor arguments and +# sending it through a socket. + +# Currently, only keyboard keys are supported so the key codes used +# below are the same key codes used in guilib/common/SDLKeyboard.cpp + +# In effect, anything that can be done with the keyboard can be done +# using the event client. + +# import the XBMC client library +# NOTE: The library is not complete yet but is usable at this stage. + +import os +from socket import * +import sys + +if os.path.exists("../../lib/python"): + # try loading modules from source directory + sys.path.append("../../lib/python") + + from xbmcclient import * + + ICON_PATH = "../../icons/" +else: + # fallback to system wide modules + + from kodi.xbmcclient import * + from kodi.defs import * + +def main(): + import time + import sys + + # connect to localhost, port 9777 using a UDP socket + # this only needs to be done once. + # by default this is where XBMC will be listening for incoming + # connections. + host = "localhost" + port = 9777 + addr = (host, port) + sock = socket(AF_INET,SOCK_DGRAM) + + # First packet must be HELO (no it's not a typo) and can contain an icon + # 'icon_type' can be one of ICON_NONE, ICON_PNG, ICON_JPG or ICON_GIF + packet = PacketHELO(devicename="Example Remote", + icon_type=ICON_PNG, + icon_file=ICON_PATH + "/bluetooth.png") + packet.send(sock, addr) + + # IMPORTANT: After a HELO packet is sent, the client needs to "ping" XBMC + # at least once every 60 seconds or else the client will time out. + # Every valid packet sent to XBMC acts as a ping, however if no valid + # packets NEED to be sent (eg. the user hasn't pressed a key in 50 seconds) + # then you can use the PacketPING class to send a ping packet (which is + # basically just an empty packet). See below. + + # Once a client times out, it will need to reissue the HELO packet. + # Currently, since this is a unidirectional protocol, there is no way + # for the client to know if it has timed out. + + # wait for notification window to close (in XBMC) + time.sleep(5) + + # press 'S' + packet = PacketBUTTON(code='S', queue=1) + packet.send(sock, addr) + + # wait for a few seconds + time.sleep(2) + + # press the enter key (13 = enter) + packet = PacketBUTTON(code=13, queue=1) + packet.send(sock, addr) + + # BYE is not required since XBMC would have shut down + packet = PacketBYE() # PacketPING if you want to ping + packet.send(sock, addr) + +if __name__=="__main__": + main() diff --git a/tools/EventClients/examples/python/example_button2.py b/tools/EventClients/examples/python/example_button2.py new file mode 100755 index 0000000..bb91704 --- /dev/null +++ b/tools/EventClients/examples/python/example_button2.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 + +# This is a simple example showing how you can send a key press event +# to XBMC in a non-queued fashion to achieve a button pressed down +# event i.e. a key press that repeats. + +# The repeat interval is currently hard coded in XBMC but that might +# change in the future. + +# NOTE: Read the comments in 'example_button1.py' for a more detailed +# explanation. + +import os +from socket import * +import sys + +if os.path.exists("../../lib/python"): + # try loading modules from source directory + sys.path.append("../../lib/python") + + from xbmcclient import * + + ICON_PATH = "../../icons/" +else: + # fallback to system wide modules + + from kodi.xbmcclient import * + from kodi.defs import * + +def main(): + import time + import sys + + host = "localhost" + port = 9777 + addr = (host, port) + + sock = socket(AF_INET,SOCK_DGRAM) + + # First packet must be HELO and can contain an icon + packet = PacketHELO("Example Remote", ICON_PNG, + ICON_PATH + "/bluetooth.png") + packet.send(sock, addr) + + # wait for notification window to close (in XBMC) + time.sleep(5) + + # send a up key press using the xbox gamepad map "XG" and button + # name "dpadup" ( see PacketBUTTON doc for more details) + packet = PacketBUTTON(map_name="XG", button_name="dpadup") + packet.send(sock, addr) + + # wait for a few seconds to see its effect + time.sleep(5) + + # send a down key press using the raw keyboard code + packet = PacketBUTTON(code=0x28) + packet.send(sock, addr) + + # wait for a few seconds to see its effect + time.sleep(5) + + # send a right key press using the keyboard map "KB" and button + # name "right" + packet = PacketBUTTON(map_name="KB", button_name="right") + packet.send(sock, addr) + + # wait for a few seconds to see its effect + time.sleep(5) + + # that's enough, release the button. During release, button code + # doesn't matter. + packet = PacketBUTTON(code=0x28, down=0) + packet.send(sock, addr) + + # ok we're done, close the connection + # Note that closing the connection clears any repeat key that is + # active. So in this example, the actual release button event above + # need not have been sent. + packet = PacketBYE() + packet.send(sock, addr) + +if __name__=="__main__": + main() diff --git a/tools/EventClients/examples/python/example_mouse.py b/tools/EventClients/examples/python/example_mouse.py new file mode 100755 index 0000000..7c43782 --- /dev/null +++ b/tools/EventClients/examples/python/example_mouse.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 + +# This is a simple example showing how you can send mouse movement +# events to XBMC. + +# NOTE: Read the comments in 'example_button1.py' for a more detailed +# explanation. + +import os +from socket import * +import sys + +if os.path.exists("../../lib/python"): + # try loading modules from source directory + sys.path.append("../../lib/python") + + from xbmcclient import * + + ICON_PATH = "../../icons/" +else: + # fallback to system wide modules + + from kodi.xbmcclient import * + from kodi.defs import * + +def main(): + import time + import sys + + host = "localhost" + port = 9777 + addr = (host, port) + + sock = socket(AF_INET,SOCK_DGRAM) + + # First packet must be HELO and can contain an icon + packet = PacketHELO("Example Mouse", ICON_PNG, + ICON_PATH + "/mouse.png") + packet.send(sock, addr) + + # wait for notification window to close (in XBMC) + time.sleep(2) + + # send mouse events to take cursor from top left to bottom right of the screen + # here 0 to 65535 will map to XBMC's screen width and height. + # Specifying absolute mouse coordinates is unsupported currently. + for i in range(0, 65535, 2): + packet = PacketMOUSE(i,i) + packet.send(sock, addr) + + # ok we're done, close the connection + packet = PacketBYE() + packet.send(sock, addr) + +if __name__=="__main__": + main() diff --git a/tools/EventClients/examples/python/example_notification.py b/tools/EventClients/examples/python/example_notification.py new file mode 100755 index 0000000..fd1a82e --- /dev/null +++ b/tools/EventClients/examples/python/example_notification.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 + +# This is a simple example showing how you can show a notification +# window with a custom icon inside XBMC. It could be used by mail +# monitoring apps, calendar apps, etc. + +import os +from socket import * +import sys + +if os.path.exists("../../lib/python"): + # try loading modules from source directory + sys.path.append("../../lib/python") + + from xbmcclient import * + + ICON_PATH = "../../icons/" +else: + # fallback to system wide modules + + from kodi.xbmcclient import * + from kodi.defs import * + +def main(): + import time + import sys + + host = "localhost" + port = 9777 + addr = (host, port) + sock = socket(AF_INET,SOCK_DGRAM) + + packet = PacketHELO("Email Notifier", ICON_NONE) + packet.send(sock, addr) + + # wait for 5 seconds + time.sleep (5) + + packet = PacketNOTIFICATION("New Mail!", # caption + "RE: Check this out", # message + ICON_PNG, # optional icon type + ICON_PATH + "/mail.png") # icon file (local) + packet.send(sock, addr) + + packet = PacketBYE() + packet.send(sock, addr) + +if __name__=="__main__": + main() diff --git a/tools/EventClients/examples/python/example_simple.py b/tools/EventClients/examples/python/example_simple.py new file mode 100755 index 0000000..2d807e8 --- /dev/null +++ b/tools/EventClients/examples/python/example_simple.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 + +# This is a simple example showing how you can send a key press event +# to XBMC using the XBMCClient class + +import os +from socket import * +import sys +import time + +if os.path.exists("../../lib/python"): + # try loading modules from source directory + sys.path.append("../../lib/python") + + from xbmcclient import * + + ICON_PATH = "../../icons/" +else: + # fallback to system wide modules + + from kodi.xbmcclient import * + from kodi.defs import * + +def main(): + + host = "localhost" + port = 9777 + + # Create an XBMCClient object and connect + xbmc = XBMCClient("Example Remote", ICON_PATH + "/bluetooth.png") + xbmc.connect() + + # wait for notification window to close (in XBMC) (optional) + time.sleep(5) + + # send a up key press using the xbox gamepad map "XG" and button + # name "dpadup" ( see PacketBUTTON doc for more details) + xbmc.send_button(map="XG", button="dpadup") + + # wait for a few seconds to see its effect + time.sleep(5) + + # send a right key press using the keyboard map "KB" and button + # name "right" + xbmc.send_keyboard_button("right") + + # wait for a few seconds to see its effect + time.sleep(5) + + # that's enough, release the button. + xbmc.release_button() + + # ok we're done, close the connection + # Note that closing the connection clears any repeat key that is + # active. So in this example, the actual release button event above + # need not have been sent. + xbmc.close() + +if __name__=="__main__": + main() |