Skip to content

Commit 6a2b0b6

Browse files
authored
Cleanup hardcoded constants in library_egl.js. NFC (#20638)
1 parent cece8cd commit 6a2b0b6

File tree

2 files changed

+38
-36
lines changed

2 files changed

+38
-36
lines changed

src/library_egl.js

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
* value of 62006. (multiple creations silently return the same surface)
1616
*/
1717

18+
{{{
19+
// Magic ID for Emscripten 'default display'
20+
globalThis.eglDefaultDisplay = 62000;
21+
// Magic ID for the only EGLConfig supported by Emscripten
22+
globalThis.eglDefaultConfig = 62002;
23+
// Magic ID for Emscripten EGLContext
24+
globalThis.eglDefaultContext = 62004;
25+
}}}
26+
1827
var LibraryEGL = {
1928
$EGL__deps: ['$Browser'],
2029
$EGL: {
@@ -39,7 +48,7 @@ var LibraryEGL = {
3948
},
4049

4150
chooseConfig(display, attribList, config, config_size, numConfigs) {
42-
if (display != 62000 /* Magic ID for Emscripten 'default display' */) {
51+
if (display != {{{ eglDefaultDisplay }}}) {
4352
EGL.setErrorCode(0x3008 /* EGL_BAD_DISPLAY */);
4453
return 0;
4554
}
@@ -81,7 +90,7 @@ var LibraryEGL = {
8190
{{{ makeSetValue('numConfigs', '0', '1', 'i32') }}}; // Total number of supported configs: 1.
8291
}
8392
if (config && config_size > 0) {
84-
{{{ makeSetValue('config', '0', '62002' /* Magic ID for the only EGLConfig supported by Emscripten */, '*') }}};
93+
{{{ makeSetValue('config', '0', eglDefaultConfig /* Magic ID for the only EGLConfig supported by Emscripten */, '*') }}};
8594
}
8695

8796
EGL.setErrorCode(0x3000 /* EGL_SUCCESS */);
@@ -101,7 +110,7 @@ var LibraryEGL = {
101110
// Therefore, be lax and allow anything to be passed in, and return the magic handle to our default EGLDisplay object.
102111

103112
// if (nativeDisplayType == 0 /* EGL_DEFAULT_DISPLAY */) {
104-
return 62000; // Magic ID for Emscripten 'default display'
113+
return {{{ eglDefaultDisplay }}};
105114
// }
106115
// else
107116
// return 0; // EGL_NO_DISPLAY
@@ -110,7 +119,7 @@ var LibraryEGL = {
110119
// EGLAPI EGLBoolean EGLAPIENTRY eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor);
111120
eglInitialize__proxy: 'sync',
112121
eglInitialize: (display, majorVersion, minorVersion) => {
113-
if (display != 62000 /* Magic ID for Emscripten 'default display' */) {
122+
if (display != {{{ eglDefaultDisplay }}}) {
114123
EGL.setErrorCode(0x3008 /* EGL_BAD_DISPLAY */);
115124
return 0;
116125
}
@@ -128,7 +137,7 @@ var LibraryEGL = {
128137
// EGLAPI EGLBoolean EGLAPIENTRY eglTerminate(EGLDisplay dpy);
129138
eglTerminate__proxy: 'sync',
130139
eglTerminate: (display) => {
131-
if (display != 62000 /* Magic ID for Emscripten 'default display' */) {
140+
if (display != {{{ eglDefaultDisplay }}}) {
132141
EGL.setErrorCode(0x3008 /* EGL_BAD_DISPLAY */);
133142
return 0;
134143
}
@@ -155,11 +164,11 @@ var LibraryEGL = {
155164
// EGLAPI EGLBoolean EGLAPIENTRY eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint *value);
156165
eglGetConfigAttrib__proxy: 'sync',
157166
eglGetConfigAttrib: (display, config, attribute, value) => {
158-
if (display != 62000 /* Magic ID for Emscripten 'default display' */) {
167+
if (display != {{{ eglDefaultDisplay }}}) {
159168
EGL.setErrorCode(0x3008 /* EGL_BAD_DISPLAY */);
160169
return 0;
161170
}
162-
if (config != 62002 /* Magic ID for the only EGLConfig supported by Emscripten */) {
171+
if (config != {{{ eglDefaultConfig }}}) {
163172
EGL.setErrorCode(0x3005 /* EGL_BAD_CONFIG */);
164173
return 0;
165174
}
@@ -195,7 +204,7 @@ var LibraryEGL = {
195204
{{{ makeSetValue('value', '0', '0x3038' /* EGL_NONE */, 'i32') }}};
196205
return 1;
197206
case 0x3028: // EGL_CONFIG_ID
198-
{{{ makeSetValue('value', '0', '62002' /* Magic ID for the only EGLConfig supported by Emscripten */, 'i32') }}};
207+
{{{ makeSetValue('value', '0', eglDefaultConfig, 'i32') }}};
199208
return 1;
200209
case 0x3029: // EGL_LEVEL
201210
{{{ makeSetValue('value', '0', '0' /* Z order/depth layer for this level. Not applicable for Emscripten. */, 'i32') }}};
@@ -272,11 +281,11 @@ var LibraryEGL = {
272281
// EGLAPI EGLSurface EGLAPIENTRY eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint *attrib_list);
273282
eglCreateWindowSurface__proxy: 'sync',
274283
eglCreateWindowSurface: (display, config, win, attrib_list) => {
275-
if (display != 62000 /* Magic ID for Emscripten 'default display' */) {
284+
if (display != {{{ eglDefaultDisplay }}}) {
276285
EGL.setErrorCode(0x3008 /* EGL_BAD_DISPLAY */);
277286
return 0;
278287
}
279-
if (config != 62002 /* Magic ID for the only EGLConfig supported by Emscripten */) {
288+
if (config != {{{ eglDefaultConfig }}}) {
280289
EGL.setErrorCode(0x3005 /* EGL_BAD_CONFIG */);
281290
return 0;
282291
}
@@ -291,7 +300,7 @@ var LibraryEGL = {
291300
// EGLAPI EGLBoolean EGLAPIENTRY eglDestroySurface(EGLDisplay display, EGLSurface surface);
292301
eglDestroySurface__proxy: 'sync',
293302
eglDestroySurface: (display, surface) => {
294-
if (display != 62000 /* Magic ID for Emscripten 'default display' */) {
303+
if (display != {{{ eglDefaultDisplay }}}) {
295304
EGL.setErrorCode(0x3008 /* EGL_BAD_DISPLAY */);
296305
return 0;
297306
}
@@ -314,7 +323,7 @@ var LibraryEGL = {
314323
// EGLAPI EGLContext EGLAPIENTRY eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint *attrib_list);
315324
eglCreateContext__proxy: 'sync',
316325
eglCreateContext: (display, config, hmm, contextAttribs) => {
317-
if (display != 62000 /* Magic ID for Emscripten 'default display' */) {
326+
if (display != {{{ eglDefaultDisplay }}}) {
318327
EGL.setErrorCode(0x3008 /* EGL_BAD_DISPLAY */);
319328
return 0;
320329
}
@@ -366,7 +375,7 @@ var LibraryEGL = {
366375

367376
// Note: This function only creates a context, but it shall not make it active.
368377
GL.makeContextCurrent(null);
369-
return 62004; // Magic ID for Emscripten EGLContext
378+
return {{{ eglDefaultContext }}};
370379
} else {
371380
EGL.setErrorCode(0x3009 /* EGL_BAD_MATCH */); // By the EGL 1.4 spec, an implementation that does not support GLES2 (WebGL in this case), this error code is set.
372381
return 0; /* EGL_NO_CONTEXT */
@@ -378,11 +387,11 @@ var LibraryEGL = {
378387
// EGLAPI EGLBoolean EGLAPIENTRY eglDestroyContext(EGLDisplay dpy, EGLContext context);
379388
eglDestroyContext__proxy: 'sync',
380389
eglDestroyContext: (display, context) => {
381-
if (display != 62000 /* Magic ID for Emscripten 'default display' */) {
390+
if (display != {{{ eglDefaultDisplay }}}) {
382391
EGL.setErrorCode(0x3008 /* EGL_BAD_DISPLAY */);
383392
return 0;
384393
}
385-
if (context != 62004 /* Magic ID for Emscripten EGLContext */) {
394+
if (context != {{{ eglDefaultContext }}}) {
386395
EGL.setErrorCode(0x3006 /* EGL_BAD_CONTEXT */);
387396
return 0;
388397
}
@@ -398,7 +407,7 @@ var LibraryEGL = {
398407
// EGLAPI EGLBoolean EGLAPIENTRY eglQuerySurface(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint *value);
399408
eglQuerySurface__proxy: 'sync',
400409
eglQuerySurface: (display, surface, attribute, value) => {
401-
if (display != 62000 /* Magic ID for Emscripten 'default display' */) {
410+
if (display != {{{ eglDefaultDisplay }}}) {
402411
EGL.setErrorCode(0x3008 /* EGL_BAD_DISPLAY */);
403412
return 0;
404413
}
@@ -413,7 +422,7 @@ var LibraryEGL = {
413422
EGL.setErrorCode(0x3000 /* EGL_SUCCESS */);
414423
switch (attribute) {
415424
case 0x3028: // EGL_CONFIG_ID
416-
{{{ makeSetValue('value', '0', '62002' /* A magic value for the only EGLConfig configuration ID supported by Emscripten. */, 'i32') }}};
425+
{{{ makeSetValue('value', '0', eglDefaultConfig, 'i32') }}};
417426
return 1;
418427
case 0x3058: // EGL_LARGEST_PBUFFER
419428
// Odd EGL API: If surface is not a pbuffer surface, 'value' should not be written to. It's not specified as an error, so true should(?) be returned.
@@ -464,12 +473,12 @@ var LibraryEGL = {
464473
// EGLAPI EGLBoolean EGLAPIENTRY eglQueryContext(EGLDisplay dpy, EGLContext ctx, EGLint attribute, EGLint *value);
465474
eglQueryContext__proxy: 'sync',
466475
eglQueryContext: (display, context, attribute, value) => {
467-
if (display != 62000 /* Magic ID for Emscripten 'default display' */) {
476+
if (display != {{{ eglDefaultDisplay }}}) {
468477
EGL.setErrorCode(0x3008 /* EGL_BAD_DISPLAY */);
469478
return 0;
470479
}
471480
//\todo An EGL_NOT_INITIALIZED error is generated if EGL is not initialized for dpy.
472-
if (context != 62004 /* Magic ID for Emscripten EGLContext */) {
481+
if (context != {{{ eglDefaultContext }}}) {
473482
EGL.setErrorCode(0x3006 /* EGL_BAD_CONTEXT */);
474483
return 0;
475484
}
@@ -481,7 +490,7 @@ var LibraryEGL = {
481490
EGL.setErrorCode(0x3000 /* EGL_SUCCESS */);
482491
switch (attribute) {
483492
case 0x3028: // EGL_CONFIG_ID
484-
{{{ makeSetValue('value', '0', '62002' /* A magic value for the only EGLConfig configuration ID supported by Emscripten. */, 'i32') }}};
493+
{{{ makeSetValue('value', '0', eglDefaultConfig, 'i32') }}};
485494
return 1;
486495
case 0x3097: // EGL_CONTEXT_CLIENT_TYPE
487496
{{{ makeSetValue('value', '0', '0x30A0' /* EGL_OPENGL_ES_API */, 'i32') }}};
@@ -502,15 +511,13 @@ var LibraryEGL = {
502511

503512
// EGLAPI EGLint EGLAPIENTRY eglGetError(void);
504513
eglGetError__proxy: 'sync',
505-
eglGetError: () => {
506-
return EGL.errorCode;
507-
},
514+
eglGetError: () => EGL.errorCode,
508515

509516
// EGLAPI const char * EGLAPIENTRY eglQueryString(EGLDisplay dpy, EGLint name);
510517
eglQueryString__deps: ['$stringToNewUTF8'],
511518
eglQueryString__proxy: 'sync',
512519
eglQueryString: (display, name) => {
513-
if (display != 62000 /* Magic ID for Emscripten 'default display' */) {
520+
if (display != {{{ eglDefaultDisplay }}}) {
514521
EGL.setErrorCode(0x3008 /* EGL_BAD_DISPLAY */);
515522
return 0;
516523
}
@@ -572,7 +579,7 @@ var LibraryEGL = {
572579
eglSwapInterval__deps: ['emscripten_set_main_loop_timing'],
573580
eglSwapInterval__proxy: 'sync',
574581
eglSwapInterval: (display, interval) => {
575-
if (display != 62000 /* Magic ID for Emscripten 'default display' */) {
582+
if (display != {{{ eglDefaultDisplay }}}) {
576583
EGL.setErrorCode(0x3008 /* EGL_BAD_DISPLAY */);
577584
return 0;
578585
}
@@ -587,12 +594,12 @@ var LibraryEGL = {
587594
eglMakeCurrent__deps: ['$GL'],
588595
eglMakeCurrent__proxy: 'sync',
589596
eglMakeCurrent: (display, draw, read, context) => {
590-
if (display != 62000 /* Magic ID for Emscripten 'default display' */) {
597+
if (display != {{{ eglDefaultDisplay }}}) {
591598
EGL.setErrorCode(0x3008 /* EGL_BAD_DISPLAY */);
592599
return 0 /* EGL_FALSE */;
593600
}
594601
//\todo An EGL_NOT_INITIALIZED error is generated if EGL is not initialized for dpy.
595-
if (context != 0 && context != 62004 /* Magic ID for Emscripten EGLContext */) {
602+
if (context != 0 && context != {{{ eglDefaultContext }}}) {
596603
EGL.setErrorCode(0x3006 /* EGL_BAD_CONTEXT */);
597604
return 0;
598605
}
@@ -612,9 +619,7 @@ var LibraryEGL = {
612619

613620
// EGLAPI EGLContext EGLAPIENTRY eglGetCurrentContext(void);
614621
eglGetCurrentContext__proxy: 'sync',
615-
eglGetCurrentContext: () => {
616-
return EGL.currentContext;
617-
},
622+
eglGetCurrentContext: () => EGL.currentContext,
618623

619624
// EGLAPI EGLSurface EGLAPIENTRY eglGetCurrentSurface(EGLint readdraw);
620625
eglGetCurrentSurface__proxy: 'sync',
@@ -631,9 +636,7 @@ var LibraryEGL = {
631636

632637
// EGLAPI EGLDisplay EGLAPIENTRY eglGetCurrentDisplay(void);
633638
eglGetCurrentDisplay__proxy: 'sync',
634-
eglGetCurrentDisplay: () => {
635-
return EGL.currentContext ? 62000 /* Magic ID for Emscripten 'default display' */ : 0;
636-
},
639+
eglGetCurrentDisplay: () => EGL.currentContext ? {{{ eglDefaultDisplay }}} : 0,
637640

638641
// EGLAPI EGLBoolean EGLAPIENTRY eglSwapBuffers(EGLDisplay dpy, EGLSurface surface);
639642
eglSwapBuffers__proxy: 'sync',

src/library_xlib.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
*/
66

77
var LibraryXlib = {
8-
XOpenDisplay: (name) => {
9-
return 1; // We support 1 display, the canvas
10-
},
8+
// We support 1 display, the canvas
9+
XOpenDisplay: (name) => 1,
1110

1211
XCreateWindow__deps: ['$Browser'],
1312
XCreateWindow: (display, parent, x, y, width, height, border_width, depth, class_, visual, valuemask, attributes) => {

0 commit comments

Comments
 (0)