diff options
author | Mikhail Burakov <mburakov@mailbox.org> | 2023-04-02 20:45:16 +0200 |
---|---|---|
committer | Mikhail Burakov <mburakov@mailbox.org> | 2023-04-07 13:48:06 +0200 |
commit | 779b65bc22e0463f42161f0e03fee2cef9b2d790 (patch) | |
tree | 29d252c1615c0d792bd2b1e2daf5ea556d09e7e2 /main.c | |
parent | 0107291f780228edd3bae34cca59974a5734feda (diff) |
Implement support for keyboard events
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 38 |
1 files changed, 34 insertions, 4 deletions
@@ -29,6 +29,7 @@ #include <unistd.h> #include "decode.h" +#include "input.h" #include "toolbox/utils.h" #include "window.h" @@ -82,8 +83,25 @@ static void OnWindowClose(void* user) { g_signal = SIGINT; } +static void OnWindowFocus(void* user, bool focused) { + if (focused) return; + if (!InputStreamHandsoff(user)) { + LOG("Failed to handle window focus"); + g_signal = SIGABRT; + } +} + static void OnWindowKey(void* user, unsigned key, bool pressed) { - // TODO + if (!InputStreamKeyPress(user, key, pressed)) { + LOG("Failed to handle key press"); + g_signal = SIGABRT; + } +} + +static void InputStreamDtor(struct InputStream** input_stream) { + if (!*input_stream) return; + InputStreamDestroy(*input_stream); + *input_stream = NULL; } static void WindowDtor(struct Window** window) { @@ -99,19 +117,31 @@ static void DecodeContextDtor(struct DecodeContext** decode_context) { } int main(int argc, char* argv[]) { - if (argc < 2) { - LOG("Usage: %s <ip>:<port>", argv[0]); + if (argc < 3) { + LOG("Usage: %s <ip>:<port> <ip>:<port>", argv[0]); return EXIT_FAILURE; } int __attribute__((cleanup(SocketDtor))) sock = ConnectSocket(argv[1]); if (sock == -1) return EXIT_FAILURE; + // TODO(mburakov): Use sock instead of sock2 once backend is ready. + int __attribute__((cleanup(SocketDtor))) sock2 = ConnectSocket(argv[2]); + if (sock2 == -1) return EXIT_FAILURE; + + struct InputStream __attribute__((cleanup(InputStreamDtor)))* input_stream = + InputStreamCreate(sock2); + if (!input_stream) { + LOG("Failed to create input stream"); + return EXIT_FAILURE; + } + static const struct WindowEventHandlers window_event_handlers = { .OnClose = OnWindowClose, + .OnFocus = OnWindowFocus, .OnKey = OnWindowKey, }; struct Window __attribute__((cleanup(WindowDtor)))* window = - WindowCreate(&window_event_handlers, NULL); + WindowCreate(&window_event_handlers, input_stream); if (!window) { LOG("Failed to create window"); return EXIT_FAILURE; |