| 1 | // SPDX-FileCopyrightText: 2025 Raphael Rosch <[email protected]>cxx . . $ `<file>/src/udev_monitor.cpp`/ |
| 2 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | |
| 4 | #include "udev_monitor.hpp"cxx . . $ `<file>/src/udev_monitor.hpp`/ |
| 5 | #include <cstring> |
| 6 | #include <iostream> |
| 7 | #include <stdexcept> |
| 8 | |
| 9 | UdevMonitorcxx . . $ UdevMonitor#::UdevMonitorcxx . . $ UdevMonitor#UdevMonitor(49f6e7a06ebc5aa8).() |
| 10 | { |
| 11 | udevcxx . . $ UdevMonitor#udev. = udev_new(); |
| 12 | if (!udevcxx . . $ UdevMonitor#udev.) throw stdcxx . . $ std/runtime_error#runtime_error(6be18eeaba0abf21).::runtime_errorcxx . . $ std/runtime_error#("udev_new() failed"); |
| 13 | |
| 14 | monitorcxx . . $ UdevMonitor#monitor. = udev_monitor_new_from_netlink(udevcxx . . $ UdevMonitor#udev., "udev"); |
| 15 | if (!monitorcxx . . $ UdevMonitor#monitor.) throw stdcxx . . $ std/runtime_error#runtime_error(6be18eeaba0abf21).::runtime_errorcxx . . $ std/runtime_error#("udev_monitor_new_from_netlink() failed"); |
| 16 | if (udev_monitor_filter_add_match_subsystem_devtype(monitorcxx . . $ UdevMonitor#monitor., "input", nullptr) < 0) { |
| 17 | udev_monitor_unref(monitorcxx . . $ UdevMonitor#monitor.); |
| 18 | udev_unref(udevcxx . . $ UdevMonitor#udev.); |
| 19 | throw stdcxx . . $ std/runtime_error#runtime_error(6be18eeaba0abf21).::runtime_errorcxx . . $ std/runtime_error#("Failed to add subsystem filter"); |
| 20 | } |
| 21 | if (udev_monitor_enable_receiving(monitorcxx . . $ UdevMonitor#monitor.) < 0) { |
| 22 | udev_monitor_unref(monitorcxx . . $ UdevMonitor#monitor.); |
| 23 | udev_unref(udevcxx . . $ UdevMonitor#udev.); |
| 24 | throw stdcxx . . $ std/runtime_error#runtime_error(6be18eeaba0abf21).::runtime_errorcxx . . $ std/runtime_error#("udev monitor enable receiving failed"); |
| 25 | } |
| 26 | |
| 27 | fdcxx . . $ UdevMonitor#fd. = udev_monitor_get_fd(monitorcxx . . $ UdevMonitor#monitor.); |
| 28 | if (fdcxx . . $ UdevMonitor#fd. < 0) { |
| 29 | udev_monitor_unref(monitorcxx . . $ UdevMonitor#monitor.); |
| 30 | udev_unref(udevcxx . . $ UdevMonitor#udev.); |
| 31 | throw stdcxx . . $ std/runtime_error#runtime_error(6be18eeaba0abf21).::runtime_errorcxx . . $ std/runtime_error#("Invalid file descriptor from udev monitor"); |
| 32 | } |
| 33 | |
| 34 | auto channel = Glibcxx . . $ Glib/RefPtr#RefPtr(b50b0f543100b439).::IOChannelcxx . . $ Glib/IOChannel#::create_from_fdcxx . . $ Glib/IOChannel#create_from_fd(88c385f6404d9b7b).(fdcxx . . $ UdevMonitor#fd.); |
| 35 | channel->cxx . . $ Glib/RefPtr#`operator->`(5a2a78a048fb49a8).set_encodingcxx . . $ Glib/IOChannel#set_encoding(d1e5c33284b831e2).(); // Binary |
| 36 | channel->cxx . . $ Glib/RefPtr#`operator->`(5a2a78a048fb49a8).set_bufferedcxx . . $ Glib/IOChannel#set_buffered(b45ddcbc8423892c).(false); |
| 37 | |
| 38 | watchcxx . . $ UdevMonitor#watch. =cxx . . $ sigc/connection#`operator=`(d6e79c7c2c834e86). Glibcxx . . $ Glib/::signal_iocxx . . $ Glib/signal_io(8fdd6778f26497b6).().connectcxx . . $ Glib/SignalIO#connect(63a2cce4d6417fe4).( |
| 39 | sigccxx . . $ sigc/slot#slot(edcd8b2679e9bd1a).::mem_funcxx . . $ sigc/mem_fun(5a1f7901f4fc005e).(*this, &UdevMonitorcxx . . $ UdevMonitor#::on_io_eventcxx . . $ UdevMonitor#on_io_event(eeaa91d3ba1dceee).), |
| 40 | channel, |
| 41 | Glibcxx . . $ Glib/::IO_INcxx . . $ Glib/IO_IN.); |
| 42 | } |
| 43 | |
| 44 | UdevMonitorcxx . . $ UdevMonitor#::~cxx . . $ UdevMonitor#`~UdevMonitor`(49f6e7a06ebc5aa8).UdevMonitorcxx . . $ UdevMonitor#() |
| 45 | { |
| 46 | if (watchcxx . . $ UdevMonitor#watch.) { |
| 47 | watchcxx . . $ UdevMonitor#watch..disconnectcxx . . $ sigc/connection#disconnect(49f6e7a06ebc5aa8).(); // disconnect GLib watch |
| 48 | } |
| 49 | if (monitorcxx . . $ UdevMonitor#monitor.) { |
| 50 | udev_monitor_unref(monitorcxx . . $ UdevMonitor#monitor.); |
| 51 | monitorcxx . . $ UdevMonitor#monitor. = nullptr; |
| 52 | } |
| 53 | if (udevcxx . . $ UdevMonitor#udev.) { |
| 54 | udev_unref(udevcxx . . $ UdevMonitor#udev.); |
| 55 | udevcxx . . $ UdevMonitor#udev. = nullptr; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | bool UdevMonitorcxx . . $ UdevMonitor#::on_io_eventcxx . . $ UdevMonitor#on_io_event(eeaa91d3ba1dceee).(Glibcxx . . $ Glib/::IOConditioncxx . . $ Glib/IOCondition#) |
| 60 | { |
| 61 | struct udev_device* dev = udev_monitor_receive_device(monitorcxx . . $ UdevMonitor#monitor.); |
| 62 | if (dev) { |
| 63 | const char* action = udev_device_get_action(dev); |
| 64 | const char* devnode = udev_device_get_devnode(dev); |
| 65 | if (devnode && strstr(devnode, "/js")) { |
| 66 | signal_joystick_eventcxx . . $ UdevMonitor#signal_joystick_event..emitcxx . . $ sigc/signal2#emit(23fa8672da8e2eb7).(actioncxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d). ? action : "", devnodecxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 67 | } |
| 68 | udev_device_unref(dev); |
| 69 | } |
| 70 | return true; // Keep watching |
| 71 | } |