@kmamal/sdl 0.10.14 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -960,10 +960,14 @@ Calls to [`render()`](#windowrenderwidth-height-stride-format-buffer-options) wi
960
960
 
961
961
  ### window.native
962
962
 
963
- * `<any>`
963
+ * `<object>`
964
+ * `handle : <Buffer>` The platform-specific handle of the window.
965
+
966
+ The native type of `handle` is HWND on Windows, NSView* on macOS, and Window (unsigned long) on Linux.
967
+ It should work exactly like the [`win.getNativeWindowHandle()`](https://www.electronjs.org/docs/latest/api/browser-window#wingetnativewindowhandle) electron method.
964
968
 
965
- Holds a copy of the native (platform-specific) representation of the window.
966
- Only used for passing to [@kmamal/gl](https://github.com/kmamal/headless-gl#readme) or [@kmamal/gpu](https://github.com/kmamal/gpu#readme).
969
+ This object might also include extra fields that are used for passing to [@kmamal/gl](https://github.com/kmamal/headless-gl#readme) or [@kmamal/gpu](https://github.com/kmamal/gpu#readme).
970
+ Please ignore these.
967
971
 
968
972
  ### window.maximized
969
973
 
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.10.14",
2
+ "version": "0.11.1",
3
3
  "name": "@kmamal/sdl",
4
4
  "description": "SDL bindings for Node.js",
5
5
  "keywords": [
@@ -3,10 +3,13 @@
3
3
 
4
4
 
5
5
  #ifndef __OBJC__
6
+ class NSView;
6
7
  class CALayer;
7
8
  #endif
8
9
 
9
10
 
11
+ extern "C" NSView *getCocoaWindowHandle(NSWindow *);
12
+
10
13
  extern "C" CALayer *getCocoaGlView(NSWindow *);
11
14
 
12
15
  extern "C" CALayer *getCocoaGpuView(NSWindow *);
@@ -4,6 +4,12 @@
4
4
  #include "cocoa-window.h"
5
5
 
6
6
 
7
+ extern "C" NSView *getCocoaWindowHandle(NSWindow *window) {
8
+ @autoreleasepool {
9
+ return [window contentView];
10
+ }
11
+ }
12
+
7
13
  extern "C" CALayer *getCocoaGlView(NSWindow *window) {
8
14
  @autoreleasepool {
9
15
  NSView *view = [window contentView];
@@ -6,6 +6,7 @@
6
6
 
7
7
 
8
8
  #if defined(__LINUX__)
9
+ #define NativeWindowHandle Window
9
10
  #define GL_NativeWindow Window
10
11
  struct GPU_NativeData {
11
12
  Display *display;
@@ -13,6 +14,7 @@
13
14
  };
14
15
  #define GPU_WINDOW_FLAG SDL_WINDOW_VULKAN
15
16
  #elif defined(__WIN32__)
17
+ #define NativeWindowHandle HWND
16
18
  #define GL_NativeWindow HWND
17
19
  struct GPU_NativeData {
18
20
  HWND hwnd;
@@ -21,6 +23,7 @@
21
23
  #define GPU_WINDOW_FLAG 0
22
24
  #elif defined(__MACOSX__)
23
25
  #include "cocoa-window.h"
26
+ #define NativeWindowHandle NSView *
24
27
  #define GL_NativeWindow CALayer *
25
28
  struct GPU_NativeData {
26
29
  CALayer *layer;
@@ -150,52 +153,61 @@ window::create (const Napi::CallbackInfo &info)
150
153
  int pixel_width, pixel_height;
151
154
  SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
152
155
 
153
- Napi::Value native;
156
+ Napi::Object native = Napi::Object::New(env);
154
157
 
155
- if (is_opengl || is_webgpu) {
156
- SDL_SysWMinfo info;
157
- SDL_VERSION(&(info.version));
158
- bool got_info = SDL_GetWindowWMInfo(window, &info);
159
- if (got_info == SDL_FALSE) {
160
- std::ostringstream message;
161
- message << "SDL_GetWindowWMInfo(" << window_id << ") error: " << SDL_GetError();
162
- SDL_ClearError();
163
- throw Napi::Error::New(env, message.str());
164
- }
158
+ SDL_SysWMinfo sys_wm_info;
159
+ SDL_VERSION(&(sys_wm_info.version));
160
+ if (SDL_GetWindowWMInfo(window, &sys_wm_info) == SDL_FALSE) {
161
+ std::ostringstream message;
162
+ message << "SDL_GetWindowWMInfo(" << window_id << ") error: " << SDL_GetError();
163
+ SDL_ClearError();
164
+ throw Napi::Error::New(env, message.str());
165
+ }
165
166
 
166
- if (is_opengl) {
167
- int native_size = sizeof(GL_NativeWindow);
168
- Napi::Buffer<char> native_buffer = Napi::Buffer<char>::New(env, native_size);
169
- GL_NativeWindow *native_window = (GL_NativeWindow *) native_buffer.Data();
170
- native = native_buffer;
171
-
172
- #if defined(__LINUX__)
173
- *native_window = info.info.x11.window;
174
- #elif defined(__WIN32__)
175
- *native_window = info.info.win.window;
176
- #elif defined(__MACOSX__)
177
- *native_window = getCocoaGlView(info.info.cocoa.window);
178
- #endif
179
- } else if (is_webgpu) {
180
- int native_size = sizeof(GPU_NativeData);
181
- Napi::Buffer<char> native_buffer = Napi::Buffer<char>::New(env, native_size);
182
- GPU_NativeData *native_data = (GPU_NativeData *) native_buffer.Data();
183
- native = native_buffer;
184
-
185
- #if defined(__LINUX__)
186
- native_data->display = info.info.x11.display;
187
- native_data->window = info.info.x11.window;
188
- #elif defined(__WIN32__)
189
- native_data->hwnd = info.info.win.window;
190
- native_data->hinstance = info.info.win.hinstance;
191
- #elif defined(__MACOSX__)
192
- native_data->layer = getCocoaGpuView(info.info.cocoa.window);
193
- #endif
194
- }
167
+ int native_handle_size = sizeof(NativeWindowHandle);
168
+ Napi::Buffer<char> native_handle_buffer = Napi::Buffer<char>::New(env, native_handle_size);
169
+ NativeWindowHandle *native_handle = (NativeWindowHandle *) native_handle_buffer.Data();
170
+ native.Set("handle", native_handle_buffer);
171
+
172
+ #if defined(__LINUX__)
173
+ *native_handle = sys_wm_info.info.x11.window;
174
+ #elif defined(__WIN32__)
175
+ *native_handle = sys_wm_info.info.win.window;
176
+ #elif defined(__MACOSX__)
177
+ *native_handle = getCocoaWindowHandle(sys_wm_info.info.cocoa.window);
178
+ #endif
179
+
180
+ if (is_opengl) {
181
+ int native_gl_size = sizeof(GL_NativeWindow);
182
+ Napi::Buffer<char> native_gl_buffer = Napi::Buffer<char>::New(env, native_gl_size);
183
+ GL_NativeWindow *native_gl = (GL_NativeWindow *) native_gl_buffer.Data();
184
+ native.Set("gl", native_gl_buffer);
185
+
186
+ #if defined(__LINUX__)
187
+ *native_gl = sys_wm_info.info.x11.window;
188
+ #elif defined(__WIN32__)
189
+ *native_gl = sys_wm_info.info.win.window;
190
+ #elif defined(__MACOSX__)
191
+ *native_gl = getCocoaGlView(sys_wm_info.info.cocoa.window);
192
+ #endif
193
+ } else if (is_webgpu) {
194
+ int native_gpu_size = sizeof(GPU_NativeData);
195
+ Napi::Buffer<char> native_gpu_buffer = Napi::Buffer<char>::New(env, native_gpu_size);
196
+ GPU_NativeData *native_gpu = (GPU_NativeData *) native_gpu_buffer.Data();
197
+ native.Set("gpu", native_gpu_buffer);
198
+
199
+ #if defined(__LINUX__)
200
+ native_gpu->display = sys_wm_info.info.x11.display;
201
+ native_gpu->window = sys_wm_info.info.x11.window;
202
+ #elif defined(__WIN32__)
203
+ native_gpu->hwnd = sys_wm_info.info.win.window;
204
+ native_gpu->hinstance = sys_wm_info.info.win.hinstance;
205
+ #elif defined(__MACOSX__)
206
+ native_gpu->layer = getCocoaGpuView(sys_wm_info.info.cocoa.window);
207
+ #endif
195
208
  }
196
209
  else {
197
210
  updateRenderer(env, window, &is_accelerated, &is_vsync);
198
- native = env.Null();
199
211
  }
200
212
 
201
213
  if (is_visible) { SDL_ShowWindow(window); }
@@ -447,7 +447,7 @@ export namespace Sdl {
447
447
 
448
448
  readonly opengl: boolean
449
449
  readonly webgpu: boolean
450
- readonly native: unknown
450
+ readonly native: { handle: Buffer }
451
451
 
452
452
  readonly maximized: boolean
453
453
  maximize (): void