Debian Codegraph @

xbmc/windowing/osx/OpenGL/OSXGLView.mm

raw
1 /*
2 * Copyright (C) 2021- Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #import "OSXGLView.h"
10
11 #include "ServiceBroker.h"
12 #include "utils/log.h"
13 #import "windowing/osx/WinSystemOSX.h"
14
15 #include "system_gl.h"
16
17 @implementation OSXGLView
18 {
19 NSOpenGLContext* m_glcontext;
20 NSTrackingArea* m_trackingArea;
21 }
22
23 - (void)SendInputEvent:(NSEvent*)nsEvent
24 {
25 CWinSystemOSX* winSystem = dynamic_cast<CWinSystemOSX*>(CServiceBroker::GetWinSystem());
26 if (winSystem)
27 {
28 winSystem->SendInputEvent(nsEvent);
29 }
30 }
31
32 - (id)initWithFrame:(NSRect)frameRect
33 {
34 // clang-format off
35 NSOpenGLPixelFormatAttribute wattrs[] = {
36 NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
37 NSOpenGLPFAAccelerated,
38 NSOpenGLPFAAlphaSize, 8,
39 NSOpenGLPFAColorSize, 32,
40 NSOpenGLPFADepthSize, 24,
41 NSOpenGLPFADoubleBuffer,
42 NSOpenGLPFANoRecovery,
43 0
44 };
45 // clang-format on
46 auto createGLContext = [&wattrs]
47 {
48 auto pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:wattrs];
49 return [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
50 };
51
52 self = [super initWithFrame:frameRect];
53 if (self)
54 {
55 m_glcontext = createGLContext();
56 if (!m_glcontext)
57 {
58 CLog::Log(LOGERROR,
59 "failed to create NSOpenGLContext, falling back to legacy OpenGL profile");
60
61 wattrs[1] = NSOpenGLProfileVersionLegacy;
62 m_glcontext = createGLContext();
63 assert(m_glcontext);
64 }
65 }
66 self.wantsBestResolutionOpenGLSurface = YES;
67 [self updateTrackingAreas];
68
69 GLint swapInterval = 1;
70 [m_glcontext setValues:&swapInterval forParameter:NSOpenGLContextParameterSwapInterval];
71 [m_glcontext makeCurrentContext];
72
73 return self;
74 }
75
76 - (void)dealloc
77 {
78 [NSOpenGLContext clearCurrentContext];
79 [m_glcontext clearDrawable];
80 }
81
82 - (BOOL)acceptsFirstResponder
83 {
84 return YES;
85 }
86
87 - (void)drawRect:(NSRect)rect
88 {
89 static dispatch_once_t onceToken;
90 dispatch_once(&onceToken, ^{
91 [self setOpenGLContext:m_glcontext];
92
93 // clear screen on first render
94 glClearColor(0, 0, 0, 0);
95 glClear(GL_COLOR_BUFFER_BIT);
96 glClearColor(0, 0, 0, 0);
97
98 [m_glcontext update];
99 });
100 }
101
102 - (void)updateTrackingAreas
103 {
104 if (m_trackingArea != nil)
105 {
106 [self removeTrackingArea:m_trackingArea];
107 }
108
109 const int opts =
110 (NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveAlways);
111 m_trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds
112 options:opts
113 owner:self
114 userInfo:nil];
115 [self addTrackingArea:m_trackingArea];
116 }
117
118 - (void)mouseEntered:(NSEvent*)nsEvent
119 {
120 CWinSystemOSX* winSystem = dynamic_cast<CWinSystemOSX*>(CServiceBroker::GetWinSystem());
121 if (winSystem)
122 winSystem->signalMouseEntered();
123 }
124
125 #pragma mark - Input Events
126
127 - (void)mouseMoved:(NSEvent*)nsEvent
128 {
129 [self SendInputEvent:nsEvent];
130 }
131
132 - (void)mouseDown:(NSEvent*)nsEvent
133 {
134 [self SendInputEvent:nsEvent];
135 }
136
137 - (void)mouseDragged:(NSEvent*)nsEvent
138 {
139 [self SendInputEvent:nsEvent];
140 }
141
142 - (void)mouseUp:(NSEvent*)nsEvent
143 {
144 [self SendInputEvent:nsEvent];
145 }
146
147 - (void)rightMouseDown:(NSEvent*)nsEvent
148 {
149 [self SendInputEvent:nsEvent];
150 }
151
152 - (void)rightMouseDragged:(NSEvent*)nsEvent
153 {
154 [self SendInputEvent:nsEvent];
155 }
156
157 - (void)rightMouseUp:(NSEvent*)nsEvent
158 {
159 [self SendInputEvent:nsEvent];
160 }
161
162 - (void)otherMouseUp:(NSEvent*)nsEvent
163 {
164 [self SendInputEvent:nsEvent];
165 }
166
167 - (void)otherMouseDown:(NSEvent*)nsEvent
168 {
169 [self SendInputEvent:nsEvent];
170 }
171
172 - (void)scrollWheel:(NSEvent*)nsEvent
173 {
174 [self SendInputEvent:nsEvent];
175 }
176
177 - (void)otherMouseDragged:(NSEvent*)nsEvent
178 {
179 [self SendInputEvent:nsEvent];
180 }
181
182 - (void)keyDown:(NSEvent*)nsEvent
183 {
184 [self SendInputEvent:nsEvent];
185 }
186
187 - (void)keyUp:(NSEvent*)nsEvent
188 {
189 [self SendInputEvent:nsEvent];
190 }
191
192 - (void)mouseExited:(NSEvent*)nsEvent
193 {
194 CWinSystemOSX* winSystem = dynamic_cast<CWinSystemOSX*>(CServiceBroker::GetWinSystem());
195 if (winSystem)
196 winSystem->signalMouseExited();
197 }
198
199 - (CGLContextObj)getGLContextObj
200 {
201 assert(m_glcontext);
202 return [m_glcontext CGLContextObj];
203 }
204
205 - (void)Update
206 {
207 assert(m_glcontext);
208 [m_glcontext makeCurrentContext];
209 [m_glcontext update];
210 }
211
212 - (void)FlushBuffer
213 {
214 assert(m_glcontext);
215 [m_glcontext makeCurrentContext];
216 [m_glcontext flushBuffer];
217 }
218 @end