fix(macos): replace depreciated AbsoluteToNanoseconds (#1986)

Co-authored-by: Cameron Gutman <2695644+cgutman@users.noreply.github.com>
This commit is contained in:
ReenigneArcher
2024-01-05 23:28:50 -05:00
committed by GitHub
parent 7d751f050e
commit 791ed48a3f

View File

@@ -3,16 +3,18 @@
* @brief todo * @brief todo
*/ */
#import <Carbon/Carbon.h> #import <Carbon/Carbon.h>
#include <chrono>
#include <mach/mach.h> #include <mach/mach.h>
#include <mach/mach_time.h>
#include "src/main.h" #include "src/main.h"
#include "src/platform/common.h" #include "src/platform/common.h"
#include "src/utility.h" #include "src/utility.h"
// Delay for a double click /**
// FIXME: we probably want to make this configurable * @brief Delay for a double click, in milliseconds.
#define MULTICLICK_DELAY_NS 500000000 * @todo Make this configurable.
*/
constexpr std::chrono::milliseconds MULTICLICK_DELAY_MS(500);
namespace platf { namespace platf {
using namespace std::literals; using namespace std::literals;
@@ -30,7 +32,7 @@ namespace platf {
// mouse related stuff // mouse related stuff
CGEventRef mouse_event; // mouse event source CGEventRef mouse_event; // mouse event source
bool mouse_down[3]; // mouse button status bool mouse_down[3]; // mouse button status
uint64_t last_mouse_event[3][2]; // timestamp of last mouse events std::chrono::steady_clock::steady_clock::time_point last_mouse_event[3][2]; // timestamp of last mouse events
}; };
// A struct to hold a Windows keycode to Mac virtual keycode mapping. // A struct to hold a Windows keycode to Mac virtual keycode mapping.
@@ -384,17 +386,6 @@ const KeyCodeMap kKeyCodesMap[] = {
post_mouse(input, kCGMouseButtonLeft, event_type_mouse(input), location, 0); post_mouse(input, kCGMouseButtonLeft, event_type_mouse(input), location, 0);
} }
uint64_t
time_diff(uint64_t start) {
uint64_t elapsed;
Nanoseconds elapsedNano;
elapsed = mach_absolute_time() - start;
elapsedNano = AbsoluteToNanoseconds(*(AbsoluteTime *) &elapsed);
return *(uint64_t *) &elapsedNano;
}
void void
button_mouse(input_t &input, int button, bool release) { button_mouse(input_t &input, int button, bool release) {
CGMouseButton mac_button; CGMouseButton mac_button;
@@ -422,15 +413,16 @@ const KeyCodeMap kKeyCodesMap[] = {
mouse->mouse_down[mac_button] = !release; mouse->mouse_down[mac_button] = !release;
// if the last mouse down was less than MULTICLICK_DELAY_NS, we send a double click event // if the last mouse down was less than MULTICLICK_DELAY_MS, we send a double click event
if (time_diff(mouse->last_mouse_event[mac_button][release]) < MULTICLICK_DELAY_NS) { auto now = std::chrono::steady_clock::now();
if (now < mouse->last_mouse_event[mac_button][release] + MULTICLICK_DELAY_MS) {
post_mouse(input, mac_button, event, get_mouse_loc(input), 2); post_mouse(input, mac_button, event, get_mouse_loc(input), 2);
} }
else { else {
post_mouse(input, mac_button, event, get_mouse_loc(input), 1); post_mouse(input, mac_button, event, get_mouse_loc(input), 1);
} }
mouse->last_mouse_event[mac_button][release] = mach_absolute_time(); mouse->last_mouse_event[mac_button][release] = now;
} }
void void
@@ -534,12 +526,6 @@ const KeyCodeMap kKeyCodesMap[] = {
macos_input->mouse_down[0] = false; macos_input->mouse_down[0] = false;
macos_input->mouse_down[1] = false; macos_input->mouse_down[1] = false;
macos_input->mouse_down[2] = false; macos_input->mouse_down[2] = false;
macos_input->last_mouse_event[0][0] = 0;
macos_input->last_mouse_event[0][1] = 0;
macos_input->last_mouse_event[1][0] = 0;
macos_input->last_mouse_event[1][1] = 0;
macos_input->last_mouse_event[2][0] = 0;
macos_input->last_mouse_event[2][1] = 0;
BOOST_LOG(debug) << "Display "sv << macos_input->display << ", pixel dimension: " << CGDisplayPixelsWide(macos_input->display) << "x"sv << CGDisplayPixelsHigh(macos_input->display); BOOST_LOG(debug) << "Display "sv << macos_input->display << ", pixel dimension: " << CGDisplayPixelsWide(macos_input->display) << "x"sv << CGDisplayPixelsHigh(macos_input->display);