bool DisplayMessageQueue_X11::process(int timeout_ms) { auto end_time = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms); while (true) { process_message(); process_queued_events(); // What is this? If its related to Event then it should be removed process_window_sockets(); // Same for this thing if (end_time <= std::chrono::steady_clock::now()) break; int x11_handle = ConnectionNumber(display); struct timeval tv; if (timeout_ms > 0) { tv.tv_sec = timeout_ms / 1000; tv.tv_usec = (timeout_ms % 1000) * 1000; } else if (timeout_ms == 0) { tv.tv_sec = 0; tv.tv_usec = 0; } else { tv.tv_sec = 0x7FFFFFFF; tv.tv_usec = 0; } fd_set rfds; FD_ZERO(&rfds); FD_SET(x11_handle, &rfds); FD_SET(async_work_event.read_fd(), &rfds); FD_SET(exit_event.read_fd(), &rfds); int result = select(std::max(std::max(async_work_event.read_fd(), x11_handle), exit_event.read_fd()) + 1, &rfds, nullptr, nullptr, &tv); if (result > 0) { if (FD_ISSET(async_work_event.read_fd(), &rfds)) { async_work_event.reset(); process_async_work(); } if (FD_ISSET(exit_event.read_fd(), &rfds)) { exit_event.reset(); return false; } } else { break; } } return true; }
void udb_run(void) { uint16_t currentTime; uint16_t nextHeartbeatTime; if (strlen(SILSIM_SERIAL_RC_INPUT_DEVICE) == 0) { udb_pwIn[THROTTLE_INPUT_CHANNEL] = 2000; udb_pwTrim[THROTTLE_INPUT_CHANNEL] = 2000; } nextHeartbeatTime = get_current_milliseconds(); while (1) { if (!handleUDBSockets()) { sleep_milliseconds(1); } currentTime = get_current_milliseconds(); if (currentTime >= nextHeartbeatTime && !(nextHeartbeatTime <= UDB_STEP_TIME && currentTime >= UDB_WRAP_TIME-UDB_STEP_TIME)) { udb_callback_read_sensors(); udb_flags._.radio_on = (sil_radio_on && udb_pwIn[FAILSAFE_INPUT_CHANNEL] >= FAILSAFE_INPUT_MIN && udb_pwIn[FAILSAFE_INPUT_CHANNEL] <= FAILSAFE_INPUT_MAX); LED_GREEN = (udb_flags._.radio_on) ? LED_ON : LED_OFF ; if (udb_heartbeat_counter % 20 == 0) udb_background_callback_periodic(); // Run at 2Hz udb_servo_callback_prepare_outputs(); sil_ui_update(); if (udb_heartbeat_counter % 80 == 0) writeEEPROMFileIfNeeded(); // Run at 0.5Hz udb_heartbeat_counter++; nextHeartbeatTime = nextHeartbeatTime + UDB_STEP_TIME; if (nextHeartbeatTime > UDB_WRAP_TIME) nextHeartbeatTime -= UDB_WRAP_TIME; } process_queued_events(); } }
void udb_run(void) { uint16_t currentTime; static uint16_t nextHeartbeatTime; if (!initialised) { initialised = 1; if (strlen(SILSIM_SERIAL_RC_INPUT_DEVICE) == 0) { udb_pwIn[THROTTLE_INPUT_CHANNEL] = 2000; udb_pwTrim[THROTTLE_INPUT_CHANNEL] = 2000; } nextHeartbeatTime = get_current_milliseconds(); } // while (1) { if (!handleUDBSockets()) { sleep_milliseconds(1); } currentTime = get_current_milliseconds(); if (currentTime >= nextHeartbeatTime && !(nextHeartbeatTime <= UDB_STEP_TIME && currentTime >= UDB_WRAP_TIME-UDB_STEP_TIME)) { udb_callback_read_sensors(); udb_flags._.radio_on = (sil_radio_on && udb_pwIn[FAILSAFE_INPUT_CHANNEL] >= FAILSAFE_INPUT_MIN && udb_pwIn[FAILSAFE_INPUT_CHANNEL] <= FAILSAFE_INPUT_MAX); // LED_GREEN = (udb_flags._.radio_on) ? LED_ON : LED_OFF; if (udb_flags._.radio_on) { led_on(LED_GREEN); } else { led_off(LED_GREEN); } udb_heartbeat_40hz_callback(); // Run at 40Hz udb_heartbeat_callback(); // Run at HEARTBEAT_HZ sil_ui_update(); // if (udb_heartbeat_counter % 80 == 0) if (udb_heartbeat_counter % (2 * HEARTBEAT_HZ) == 0) { writeEEPROMFileIfNeeded(); // Run at 0.5Hz } udb_heartbeat_counter++; nextHeartbeatTime = nextHeartbeatTime + UDB_STEP_TIME; if (nextHeartbeatTime > UDB_WRAP_TIME) nextHeartbeatTime -= UDB_WRAP_TIME; } process_queued_events(); // } }
int DisplayMessageQueue_X11::wait(const std::vector<Event> &events, int timeout) { process_queued_events(); int num_events = 0; for (std::vector<Event>::size_type index_events = 0; index_events < events.size(); ++index_events) { bool flagged = events[index_events].get_event_provider()->check_before_wait(); if (flagged) return index_events; num_events += events[index_events].get_event_provider()->get_num_event_handles(); } if (num_events == 0) { std::vector<SocketMessage_X11> empty_handles; int result = msg_wait_for_multiple_objects(empty_handles, timeout); if (result == 0) // X11 message found { process_message(); return events.size(); } } else { std::vector<SocketMessage_X11> handles; for (std::vector<Event>::size_type index_events = 0; index_events < events.size(); ++index_events) { int num_handles = events[index_events].get_event_provider()->get_num_event_handles(); for (int i=0; i<num_handles; i++) { EventProvider *provider = events[index_events].get_event_provider(); if (provider == 0) throw Exception("EventProvider is a null pointer!"); SocketMessage_X11 msg; msg.handle = provider->get_event_handle(i); msg.type = provider->get_event_type(i); handles.push_back(msg); } } while (true) { int index = msg_wait_for_multiple_objects(handles, timeout); if (index < 0) break; if (index == num_events) { process_message(); return events.size(); } for (std::vector<Event>::size_type index_events = 0; index_events < events.size(); ++index_events) { int num_handles = events[index_events].get_event_provider()->get_num_event_handles(); if (index < num_handles) { bool flagged = events[index_events].get_event_provider()->check_after_wait(index); if (flagged) return index_events; break; } index -= num_handles; } } } return -1; }