|
18 | 18 | #include <LibCore/Event.h> |
19 | 19 | #include <LibCore/EventLoop.h> |
20 | 20 | #include <LibCore/File.h> |
| 21 | +#include <LibCore/FileWatcher.h> |
21 | 22 | #include <LibCore/System.h> |
| 23 | +#include <LibCore/Timer.h> |
22 | 24 | #include <LibMain/Main.h> |
23 | 25 | #include <errno.h> |
24 | 26 | #include <fcntl.h> |
@@ -124,20 +126,30 @@ static ErrorOr<void> activate_services(Core::ConfigFile const& config) |
124 | 126 | static ErrorOr<void> activate_base_services_based_on_system_mode() |
125 | 127 | { |
126 | 128 | if (g_system_mode == graphical_system_mode) { |
127 | | - bool found_gpu_device = false; |
128 | | - for (int attempt = 0; attempt < 10; attempt++) { |
129 | | - struct stat file_state; |
130 | | - int rc = lstat("/dev/gpu/connector0", &file_state); |
131 | | - if (rc == 0) { |
132 | | - found_gpu_device = true; |
133 | | - break; |
134 | | - } |
135 | | - sleep(1); |
136 | | - } |
137 | | - if (!found_gpu_device) { |
| 129 | + bool done_searching_for_gpu = false; |
| 130 | + |
| 131 | + auto timeout = Core::Timer::create_single_shot(10000, [&]() { |
138 | 132 | dbgln("WARNING: No device nodes at /dev/gpu/ directory after 10 seconds. This is probably a sign of disabled graphics functionality."); |
139 | 133 | dbgln("To cope with this, graphical mode will not be enabled."); |
140 | 134 | g_system_mode = text_system_mode; |
| 135 | + |
| 136 | + done_searching_for_gpu = true; |
| 137 | + }); |
| 138 | + |
| 139 | + auto watcher = TRY(Core::FileWatcher::create()); |
| 140 | + watcher->on_change = [&](Core::FileWatcherEvent const& event) { |
| 141 | + if (event.event_path != "/dev/gpu/connector0"sv) |
| 142 | + return; |
| 143 | + done_searching_for_gpu = true; |
| 144 | + }; |
| 145 | + |
| 146 | + TRY(watcher->add_watch("/dev/gpu/", Core::FileWatcherEvent::Type::ChildCreated)); |
| 147 | + |
| 148 | + // The GPU might have appeared while we were setting up the watcher. |
| 149 | + // Only wait for the file if we can't stat it. |
| 150 | + if (Core::System::lstat("/dev/gpu/connector0"sv).is_error()) { |
| 151 | + timeout->start(); |
| 152 | + Core::EventLoop::current().spin_until([&]() { return done_searching_for_gpu; }); |
141 | 153 | } |
142 | 154 | } |
143 | 155 |
|
|
0 commit comments