GLVis  v4.2
Accurate and flexible finite element visualization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
sdl_mac.mm
Go to the documentation of this file.
1 // Copyright (c) 2010-2022, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-443271.
4 //
5 // This file is part of the GLVis visualization tool and library. For more
6 // information and source code availability see https://glvis.org.
7 //
8 // GLVis is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11 
12 #include "sdl_mac.hpp"
13 #import <Cocoa/Cocoa.h>
14 
15 #include <unordered_map>
16 #include <mutex>
17 
18 #ifdef __clang__
19 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
20 #endif
21 
22 std::unordered_map<int, NSOpenGLContext*>& GetContextMap()
23 {
24  static std::unordered_map<int, NSOpenGLContext*> inst;
25  return inst;
26 }
27 
28 inline bool AtLeastVersion(SDL_version sdl_ver, int major, int minor, int patch)
29 {
30  return ((sdl_ver.major > major) ||
31  (sdl_ver.major == major && sdl_ver.minor > minor) ||
32  (sdl_ver.major == major && sdl_ver.minor == minor && sdl_ver.patch >= patch));
33 }
34 
36 {
37  @autoreleasepool
38  {
39  // We timeout the poll call after 500ms, just in case a pending SDL_QUIT
40  // needs to be pumped into the SDL event queue
41  [NSApp nextEventMatchingMask:NSEventMaskAny
42  untilDate:[NSDate dateWithTimeIntervalSinceNow:0.500]
43  inMode:NSDefaultRunLoopMode
44  dequeue:NO];
45  }
46 }
47 
49 {
50  @autoreleasepool
51  {
52  NSPoint loc = {0., 0.};
53  [NSApp postEvent:[NSEvent otherEventWithType:NSEventTypeApplicationDefined
54  location:loc
55  modifierFlags:0
56  timestamp:0.0
57  windowNumber:0
58  context:nil
59  subtype:0
60  data1:0
61  data2:0]
62  atStart:NO];
63  }
64 }
65 
67 {
68  static bool first_call = true;
69  static bool value = false;
70  if (first_call)
71  {
72  SDL_version sdl_ver;
73  SDL_GetVersion(&sdl_ver);
74  value = !AtLeastVersion(sdl_ver, 2, 0, 14);
75  first_call = false;
76  }
77  return value;
78 }
79 
81 {
82  @autoreleasepool
83  {
84  NSOpenGLContext* ctx = [NSOpenGLContext currentContext];
85  // This calls [SDLOpenGLContext update] defined in SDL_cocoaopengl.m
86  dispatch_sync(dispatch_get_main_queue(), ^{ [ctx update]; });
87  }
88 }
89 
91 {
92  @autoreleasepool
93  {
94  NSOpenGLContext* ctx = [NSOpenGLContext currentContext];
95  GetContextMap().emplace(wnd_id, ctx);
96  [NSOpenGLContext clearCurrentContext];
97  }
98 }
99 
101 {
102  @autoreleasepool
103  {
104  NSOpenGLContext* ctx = GetContextMap()[wnd_id];
105  [ctx makeCurrentContext];
106  }
107 }
108 
109 std::mutex swap_mtx;
110 
112 {
113  @autoreleasepool
114  {
115  std::lock_guard<std::mutex> lk{swap_mtx};
116  [[NSOpenGLContext currentContext] flushBuffer];
117  }
118 }
void ClearCurrentContext(int wnd_id)
Definition: sdl_mac.mm:90
void SwapWindow()
Definition: sdl_mac.mm:111
bool UseThreadWorkaround() const
Definition: sdl_mac.mm:66
void SetCurrentContext(int wnd_id)
Definition: sdl_mac.mm:100
bool AtLeastVersion(SDL_version sdl_ver, int major, int minor, int patch)
Definition: sdl_mac.mm:28
std::mutex swap_mtx
Definition: sdl_mac.mm:109
void SendEvent()
Definition: sdl_mac.mm:48
std::unordered_map< int, NSOpenGLContext * > & GetContextMap()
Definition: sdl_mac.mm:22
void ContextUpdate()
Definition: sdl_mac.mm:80
void WaitEvent()
Definition: sdl_mac.mm:35