Skip to content

Commit

Permalink
[osx] Fix events handling (fix #89)
Browse files Browse the repository at this point in the history
  • Loading branch information
martincapello committed May 31, 2024
1 parent 4a2e2bb commit e0b4636
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
5 changes: 2 additions & 3 deletions os/osx/event_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
#include "os/event.h"
#include "os/event_queue.h"

#include <atomic>

namespace os {

class EventQueueOSX : public EventQueue {
Expand All @@ -29,7 +27,8 @@ class EventQueueOSX : public EventQueue {
void wakeUpQueue();

base::concurrent_queue<Event> m_events;
std::atomic<bool> m_sleeping;
mutable std::mutex m_mutex;
bool m_sleeping;
};

using EventQueueImpl = EventQueueOSX;
Expand Down
22 changes: 14 additions & 8 deletions os/osx/event_queue.mm
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,21 @@
}
} while (event);

m_mutex.lock();
if (!m_events.try_pop(ev)) {
if (timeout == kWithoutTimeout)
EV_TRACE("EV: Waiting for events\n");

// Wait until there is a Cocoa event in queue
m_sleeping = true;
event = [app nextEventMatchingMask:NSEventMaskAny
untilDate:untilDate
inMode:NSDefaultRunLoopMode
dequeue:YES];
m_sleeping = false;
m_sleeping = true;
m_mutex.unlock();

// Wait until there is a Cocoa event in queue
event = [app nextEventMatchingMask:NSEventMaskAny
untilDate:untilDate
inMode:NSDefaultRunLoopMode
dequeue:YES];
m_mutex.lock();
m_sleeping = false;
m_mutex.unlock();

if (event) {
EV_TRACE("EV: Event received!\n");
Expand All @@ -94,11 +98,13 @@
EV_TRACE("EV: Timeout!");
}
}
m_mutex.unlock();
}
}

void EventQueueOSX::queueEvent(const Event& ev)
{
const std::lock_guard lock(m_mutex);
if (m_sleeping) {
// Wake up the macOS event queue. This is necessary in case that we
// change the display color profile from macOS settings: the
Expand Down

0 comments on commit e0b4636

Please sign in to comment.