From 26545aa6efcb08ba67d380fe959b9a66fcaffab8 Mon Sep 17 00:00:00 2001 From: Fierelier Date: Sat, 13 Jul 2024 06:36:27 +0200 Subject: [PATCH] Initial commit --- LICENSE | 9 ++++++ input-to-pipe | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 LICENSE create mode 100755 input-to-pipe diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6a6f3d8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2024 + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/input-to-pipe b/input-to-pipe new file mode 100755 index 0000000..298df95 --- /dev/null +++ b/input-to-pipe @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 +import sdl2 +import sdl2.ext +import sys +import os + +def main(): + pressedKeys = {} + grabbed = False + padding = b'\x00' * 9 + unbufferedStdout = os.fdopen(sys.stdout.fileno(),"wb",0) + + window = sdl2.SDL_CreateWindow("input-to-pipe".encode("utf-8"),sdl2.SDL_WINDOWPOS_UNDEFINED,sdl2.SDL_WINDOWPOS_UNDEFINED,50,50,0) + while True: + events = sdl2.ext.get_events() + for event in events: + if event.type == sdl2.SDL_QUIT: + return + + # KEYBOARD + if event.type in [sdl2.SDL_KEYDOWN,sdl2.SDL_KEYUP]: + down = (event.type == sdl2.SDL_KEYDOWN) + keycode = event.key.keysym.scancode + if down: + pressedKeys[keycode] = True + else: + del pressedKeys[keycode] + + if ( + 224 in pressedKeys and + 226 in pressedKeys and + 11 in pressedKeys + ): + grabbed = (not grabbed) + sdl2.SDL_SetRelativeMouseMode(grabbed) + sdl2.SDL_SetWindowKeyboardGrab(window,grabbed) + + unbufferedStdout.write( + padding+ + (1).to_bytes(1,"little")+ + down.to_bytes(1,"little")+ + keycode.to_bytes(1,"little") + ) + + # MOUSE + if event.type == sdl2.SDL_MOUSEMOTION: + x = event.motion.xrel + y = event.motion.yrel + unbufferedStdout.write( + padding+ + (2).to_bytes(1,"little")+ + x.to_bytes(4,"little",signed=True)+ + y.to_bytes(4,"little",signed=True) + ) + + if event.type in [sdl2.SDL_MOUSEBUTTONDOWN,sdl2.SDL_MOUSEBUTTONUP]: + down = (event.type == sdl2.SDL_MOUSEBUTTONDOWN) + keycode = event.button.button + unbufferedStdout.write( + padding+ + (3).to_bytes(1,"little")+ + down.to_bytes(1,"little")+ + keycode.to_bytes(1,"little") + ) + + if event.type == sdl2.SDL_MOUSEWHEEL: + x = event.wheel.x + y = event.wheel.y + unbufferedStdout.write( + padding+ + (4).to_bytes(1,"little")+ + x.to_bytes(4,"little",signed=True)+ + y.to_bytes(4,"little",signed=True) + ) + + #sdl2.SDL_UpdateWindowSurface(window) + sdl2.SDL_Delay(16) + +if __name__ == "__main__": + main()