DSCF

.co.uk

PC32

A ColorForth compiler and editor for x86-64
git clone https://dscf.co.uk/clone/PC32.git
Log | Files | Refs | README

monitor.c (14979B)


      1 //========================================================================
      2 // GLFW 3.3 - www.glfw.org
      3 //------------------------------------------------------------------------
      4 // Copyright (c) 2002-2006 Marcus Geelnard
      5 // Copyright (c) 2006-2019 Camilla Löwy <elmindreda@glfw.org>
      6 //
      7 // This software is provided 'as-is', without any express or implied
      8 // warranty. In no event will the authors be held liable for any damages
      9 // arising from the use of this software.
     10 //
     11 // Permission is granted to anyone to use this software for any purpose,
     12 // including commercial applications, and to alter it and redistribute it
     13 // freely, subject to the following restrictions:
     14 //
     15 // 1. The origin of this software must not be misrepresented; you must not
     16 //    claim that you wrote the original software. If you use this software
     17 //    in a product, an acknowledgment in the product documentation would
     18 //    be appreciated but is not required.
     19 //
     20 // 2. Altered source versions must be plainly marked as such, and must not
     21 //    be misrepresented as being the original software.
     22 //
     23 // 3. This notice may not be removed or altered from any source
     24 //    distribution.
     25 //
     26 //========================================================================
     27 // Please use C89 style variable declarations in this file because VS 2010
     28 //========================================================================
     29 
     30 #include "internal.h"
     31 
     32 #include <assert.h>
     33 #include <math.h>
     34 #include <float.h>
     35 #include <string.h>
     36 #include <stdlib.h>
     37 #include <limits.h>
     38 
     39 
     40 // Lexically compare video modes, used by qsort
     41 //
     42 static int compareVideoModes(const void* fp, const void* sp)
     43 {
     44     const GLFWvidmode* fm = fp;
     45     const GLFWvidmode* sm = sp;
     46     const int fbpp = fm->redBits + fm->greenBits + fm->blueBits;
     47     const int sbpp = sm->redBits + sm->greenBits + sm->blueBits;
     48     const int farea = fm->width * fm->height;
     49     const int sarea = sm->width * sm->height;
     50 
     51     // First sort on color bits per pixel
     52     if (fbpp != sbpp)
     53         return fbpp - sbpp;
     54 
     55     // Then sort on screen area
     56     if (farea != sarea)
     57         return farea - sarea;
     58 
     59     // Then sort on width
     60     if (fm->width != sm->width)
     61         return fm->width - sm->width;
     62 
     63     // Lastly sort on refresh rate
     64     return fm->refreshRate - sm->refreshRate;
     65 }
     66 
     67 // Retrieves the available modes for the specified monitor
     68 //
     69 static GLFWbool refreshVideoModes(_GLFWmonitor* monitor)
     70 {
     71     int modeCount;
     72     GLFWvidmode* modes;
     73 
     74     if (monitor->modes)
     75         return GLFW_TRUE;
     76 
     77     modes = _glfwPlatformGetVideoModes(monitor, &modeCount);
     78     if (!modes)
     79         return GLFW_FALSE;
     80 
     81     qsort(modes, modeCount, sizeof(GLFWvidmode), compareVideoModes);
     82 
     83     free(monitor->modes);
     84     monitor->modes = modes;
     85     monitor->modeCount = modeCount;
     86 
     87     return GLFW_TRUE;
     88 }
     89 
     90 
     91 //////////////////////////////////////////////////////////////////////////
     92 //////                         GLFW event API                       //////
     93 //////////////////////////////////////////////////////////////////////////
     94 
     95 // Notifies shared code of a monitor connection or disconnection
     96 //
     97 void _glfwInputMonitor(_GLFWmonitor* monitor, int action, int placement)
     98 {
     99     if (action == GLFW_CONNECTED)
    100     {
    101         _glfw.monitorCount++;
    102         _glfw.monitors =
    103             realloc(_glfw.monitors, sizeof(_GLFWmonitor*) * _glfw.monitorCount);
    104 
    105         if (placement == _GLFW_INSERT_FIRST)
    106         {
    107             memmove(_glfw.monitors + 1,
    108                     _glfw.monitors,
    109                     ((size_t) _glfw.monitorCount - 1) * sizeof(_GLFWmonitor*));
    110             _glfw.monitors[0] = monitor;
    111         }
    112         else
    113             _glfw.monitors[_glfw.monitorCount - 1] = monitor;
    114     }
    115     else if (action == GLFW_DISCONNECTED)
    116     {
    117         int i;
    118         _GLFWwindow* window;
    119 
    120         for (window = _glfw.windowListHead;  window;  window = window->next)
    121         {
    122             if (window->monitor == monitor)
    123             {
    124                 int width, height, xoff, yoff;
    125                 _glfwPlatformGetWindowSize(window, &width, &height);
    126                 _glfwPlatformSetWindowMonitor(window, NULL, 0, 0, width, height, 0);
    127                 _glfwPlatformGetWindowFrameSize(window, &xoff, &yoff, NULL, NULL);
    128                 _glfwPlatformSetWindowPos(window, xoff, yoff);
    129             }
    130         }
    131 
    132         for (i = 0;  i < _glfw.monitorCount;  i++)
    133         {
    134             if (_glfw.monitors[i] == monitor)
    135             {
    136                 _glfw.monitorCount--;
    137                 memmove(_glfw.monitors + i,
    138                         _glfw.monitors + i + 1,
    139                         ((size_t) _glfw.monitorCount - i) * sizeof(_GLFWmonitor*));
    140                 break;
    141             }
    142         }
    143     }
    144 
    145     if (_glfw.callbacks.monitor)
    146         _glfw.callbacks.monitor((GLFWmonitor*) monitor, action);
    147 
    148     if (action == GLFW_DISCONNECTED)
    149         _glfwFreeMonitor(monitor);
    150 }
    151 
    152 // Notifies shared code that a full screen window has acquired or released
    153 // a monitor
    154 //
    155 void _glfwInputMonitorWindow(_GLFWmonitor* monitor, _GLFWwindow* window)
    156 {
    157     monitor->window = window;
    158 }
    159 
    160 
    161 //////////////////////////////////////////////////////////////////////////
    162 //////                       GLFW internal API                      //////
    163 //////////////////////////////////////////////////////////////////////////
    164 
    165 // Allocates and returns a monitor object with the specified name and dimensions
    166 //
    167 _GLFWmonitor* _glfwAllocMonitor(const char* name, int widthMM, int heightMM)
    168 {
    169     _GLFWmonitor* monitor = calloc(1, sizeof(_GLFWmonitor));
    170     monitor->widthMM = widthMM;
    171     monitor->heightMM = heightMM;
    172 
    173     strncpy(monitor->name, name, sizeof(monitor->name) - 1);
    174 
    175     return monitor;
    176 }
    177 
    178 // Frees a monitor object and any data associated with it
    179 //
    180 void _glfwFreeMonitor(_GLFWmonitor* monitor)
    181 {
    182     if (monitor == NULL)
    183         return;
    184 
    185     _glfwPlatformFreeMonitor(monitor);
    186 
    187     _glfwFreeGammaArrays(&monitor->originalRamp);
    188     _glfwFreeGammaArrays(&monitor->currentRamp);
    189 
    190     free(monitor->modes);
    191     free(monitor);
    192 }
    193 
    194 // Allocates red, green and blue value arrays of the specified size
    195 //
    196 void _glfwAllocGammaArrays(GLFWgammaramp* ramp, unsigned int size)
    197 {
    198     ramp->red = calloc(size, sizeof(unsigned short));
    199     ramp->green = calloc(size, sizeof(unsigned short));
    200     ramp->blue = calloc(size, sizeof(unsigned short));
    201     ramp->size = size;
    202 }
    203 
    204 // Frees the red, green and blue value arrays and clears the struct
    205 //
    206 void _glfwFreeGammaArrays(GLFWgammaramp* ramp)
    207 {
    208     free(ramp->red);
    209     free(ramp->green);
    210     free(ramp->blue);
    211 
    212     memset(ramp, 0, sizeof(GLFWgammaramp));
    213 }
    214 
    215 // Chooses the video mode most closely matching the desired one
    216 //
    217 const GLFWvidmode* _glfwChooseVideoMode(_GLFWmonitor* monitor,
    218                                         const GLFWvidmode* desired)
    219 {
    220     int i;
    221     unsigned int sizeDiff, leastSizeDiff = UINT_MAX;
    222     unsigned int rateDiff, leastRateDiff = UINT_MAX;
    223     unsigned int colorDiff, leastColorDiff = UINT_MAX;
    224     const GLFWvidmode* current;
    225     const GLFWvidmode* closest = NULL;
    226 
    227     if (!refreshVideoModes(monitor))
    228         return NULL;
    229 
    230     for (i = 0;  i < monitor->modeCount;  i++)
    231     {
    232         current = monitor->modes + i;
    233 
    234         colorDiff = 0;
    235 
    236         if (desired->redBits != GLFW_DONT_CARE)
    237             colorDiff += abs(current->redBits - desired->redBits);
    238         if (desired->greenBits != GLFW_DONT_CARE)
    239             colorDiff += abs(current->greenBits - desired->greenBits);
    240         if (desired->blueBits != GLFW_DONT_CARE)
    241             colorDiff += abs(current->blueBits - desired->blueBits);
    242 
    243         sizeDiff = abs((current->width - desired->width) *
    244                        (current->width - desired->width) +
    245                        (current->height - desired->height) *
    246                        (current->height - desired->height));
    247 
    248         if (desired->refreshRate != GLFW_DONT_CARE)
    249             rateDiff = abs(current->refreshRate - desired->refreshRate);
    250         else
    251             rateDiff = UINT_MAX - current->refreshRate;
    252 
    253         if ((colorDiff < leastColorDiff) ||
    254             (colorDiff == leastColorDiff && sizeDiff < leastSizeDiff) ||
    255             (colorDiff == leastColorDiff && sizeDiff == leastSizeDiff && rateDiff < leastRateDiff))
    256         {
    257             closest = current;
    258             leastSizeDiff = sizeDiff;
    259             leastRateDiff = rateDiff;
    260             leastColorDiff = colorDiff;
    261         }
    262     }
    263 
    264     return closest;
    265 }
    266 
    267 // Performs lexical comparison between two @ref GLFWvidmode structures
    268 //
    269 int _glfwCompareVideoModes(const GLFWvidmode* fm, const GLFWvidmode* sm)
    270 {
    271     return compareVideoModes(fm, sm);
    272 }
    273 
    274 // Splits a color depth into red, green and blue bit depths
    275 //
    276 void _glfwSplitBPP(int bpp, int* red, int* green, int* blue)
    277 {
    278     int delta;
    279 
    280     // We assume that by 32 the user really meant 24
    281     if (bpp == 32)
    282         bpp = 24;
    283 
    284     // Convert "bits per pixel" to red, green & blue sizes
    285 
    286     *red = *green = *blue = bpp / 3;
    287     delta = bpp - (*red * 3);
    288     if (delta >= 1)
    289         *green = *green + 1;
    290 
    291     if (delta == 2)
    292         *red = *red + 1;
    293 }
    294 
    295 
    296 //////////////////////////////////////////////////////////////////////////
    297 //////                        GLFW public API                       //////
    298 //////////////////////////////////////////////////////////////////////////
    299 
    300 GLFWAPI GLFWmonitor** glfwGetMonitors(int* count)
    301 {
    302     assert(count != NULL);
    303 
    304     *count = 0;
    305 
    306     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    307 
    308     *count = _glfw.monitorCount;
    309     return (GLFWmonitor**) _glfw.monitors;
    310 }
    311 
    312 GLFWAPI GLFWmonitor* glfwGetPrimaryMonitor(void)
    313 {
    314     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    315 
    316     if (!_glfw.monitorCount)
    317         return NULL;
    318 
    319     return (GLFWmonitor*) _glfw.monitors[0];
    320 }
    321 
    322 GLFWAPI void glfwGetMonitorPos(GLFWmonitor* handle, int* xpos, int* ypos)
    323 {
    324     _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
    325     assert(monitor != NULL);
    326 
    327     if (xpos)
    328         *xpos = 0;
    329     if (ypos)
    330         *ypos = 0;
    331 
    332     _GLFW_REQUIRE_INIT();
    333 
    334     _glfwPlatformGetMonitorPos(monitor, xpos, ypos);
    335 }
    336 
    337 GLFWAPI void glfwGetMonitorWorkarea(GLFWmonitor* handle,
    338                                     int* xpos, int* ypos,
    339                                     int* width, int* height)
    340 {
    341     _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
    342     assert(monitor != NULL);
    343 
    344     if (xpos)
    345         *xpos = 0;
    346     if (ypos)
    347         *ypos = 0;
    348     if (width)
    349         *width = 0;
    350     if (height)
    351         *height = 0;
    352 
    353     _GLFW_REQUIRE_INIT();
    354 
    355     _glfwPlatformGetMonitorWorkarea(monitor, xpos, ypos, width, height);
    356 }
    357 
    358 GLFWAPI void glfwGetMonitorPhysicalSize(GLFWmonitor* handle, int* widthMM, int* heightMM)
    359 {
    360     _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
    361     assert(monitor != NULL);
    362 
    363     if (widthMM)
    364         *widthMM = 0;
    365     if (heightMM)
    366         *heightMM = 0;
    367 
    368     _GLFW_REQUIRE_INIT();
    369 
    370     if (widthMM)
    371         *widthMM = monitor->widthMM;
    372     if (heightMM)
    373         *heightMM = monitor->heightMM;
    374 }
    375 
    376 GLFWAPI void glfwGetMonitorContentScale(GLFWmonitor* handle,
    377                                         float* xscale, float* yscale)
    378 {
    379     _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
    380     assert(monitor != NULL);
    381 
    382     if (xscale)
    383         *xscale = 0.f;
    384     if (yscale)
    385         *yscale = 0.f;
    386 
    387     _GLFW_REQUIRE_INIT();
    388     _glfwPlatformGetMonitorContentScale(monitor, xscale, yscale);
    389 }
    390 
    391 GLFWAPI const char* glfwGetMonitorName(GLFWmonitor* handle)
    392 {
    393     _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
    394     assert(monitor != NULL);
    395 
    396     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    397     return monitor->name;
    398 }
    399 
    400 GLFWAPI void glfwSetMonitorUserPointer(GLFWmonitor* handle, void* pointer)
    401 {
    402     _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
    403     assert(monitor != NULL);
    404 
    405     _GLFW_REQUIRE_INIT();
    406     monitor->userPointer = pointer;
    407 }
    408 
    409 GLFWAPI void* glfwGetMonitorUserPointer(GLFWmonitor* handle)
    410 {
    411     _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
    412     assert(monitor != NULL);
    413 
    414     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    415     return monitor->userPointer;
    416 }
    417 
    418 GLFWAPI GLFWmonitorfun glfwSetMonitorCallback(GLFWmonitorfun cbfun)
    419 {
    420     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    421     _GLFW_SWAP_POINTERS(_glfw.callbacks.monitor, cbfun);
    422     return cbfun;
    423 }
    424 
    425 GLFWAPI const GLFWvidmode* glfwGetVideoModes(GLFWmonitor* handle, int* count)
    426 {
    427     _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
    428     assert(monitor != NULL);
    429     assert(count != NULL);
    430 
    431     *count = 0;
    432 
    433     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    434 
    435     if (!refreshVideoModes(monitor))
    436         return NULL;
    437 
    438     *count = monitor->modeCount;
    439     return monitor->modes;
    440 }
    441 
    442 GLFWAPI const GLFWvidmode* glfwGetVideoMode(GLFWmonitor* handle)
    443 {
    444     _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
    445     assert(monitor != NULL);
    446 
    447     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    448 
    449     _glfwPlatformGetVideoMode(monitor, &monitor->currentMode);
    450     return &monitor->currentMode;
    451 }
    452 
    453 GLFWAPI void glfwSetGamma(GLFWmonitor* handle, float gamma)
    454 {
    455     unsigned int i;
    456     unsigned short* values;
    457     GLFWgammaramp ramp;
    458     const GLFWgammaramp* original;
    459     assert(handle != NULL);
    460     assert(gamma > 0.f);
    461     assert(gamma <= FLT_MAX);
    462 
    463     _GLFW_REQUIRE_INIT();
    464 
    465     if (gamma != gamma || gamma <= 0.f || gamma > FLT_MAX)
    466     {
    467         _glfwInputError(GLFW_INVALID_VALUE, "Invalid gamma value %f", gamma);
    468         return;
    469     }
    470 
    471     original = glfwGetGammaRamp(handle);
    472     if (!original)
    473         return;
    474 
    475     values = calloc(original->size, sizeof(unsigned short));
    476 
    477     for (i = 0;  i < original->size;  i++)
    478     {
    479         float value;
    480 
    481         // Calculate intensity
    482         value = i / (float) (original->size - 1);
    483         // Apply gamma curve
    484         value = powf(value, 1.f / gamma) * 65535.f + 0.5f;
    485         // Clamp to value range
    486         value = _glfw_fminf(value, 65535.f);
    487 
    488         values[i] = (unsigned short) value;
    489     }
    490 
    491     ramp.red = values;
    492     ramp.green = values;
    493     ramp.blue = values;
    494     ramp.size = original->size;
    495 
    496     glfwSetGammaRamp(handle, &ramp);
    497     free(values);
    498 }
    499 
    500 GLFWAPI const GLFWgammaramp* glfwGetGammaRamp(GLFWmonitor* handle)
    501 {
    502     _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
    503     assert(monitor != NULL);
    504 
    505     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    506 
    507     _glfwFreeGammaArrays(&monitor->currentRamp);
    508     if (!_glfwPlatformGetGammaRamp(monitor, &monitor->currentRamp))
    509         return NULL;
    510 
    511     return &monitor->currentRamp;
    512 }
    513 
    514 GLFWAPI void glfwSetGammaRamp(GLFWmonitor* handle, const GLFWgammaramp* ramp)
    515 {
    516     _GLFWmonitor* monitor = (_GLFWmonitor*) handle;
    517     assert(monitor != NULL);
    518     assert(ramp != NULL);
    519     assert(ramp->size > 0);
    520     assert(ramp->red != NULL);
    521     assert(ramp->green != NULL);
    522     assert(ramp->blue != NULL);
    523 
    524     _GLFW_REQUIRE_INIT();
    525 
    526     if (ramp->size <= 0)
    527     {
    528         _glfwInputError(GLFW_INVALID_VALUE,
    529                         "Invalid gamma ramp size %i",
    530                         ramp->size);
    531         return;
    532     }
    533 
    534     if (!monitor->originalRamp.size)
    535     {
    536         if (!_glfwPlatformGetGammaRamp(monitor, &monitor->originalRamp))
    537             return;
    538     }
    539 
    540     _glfwPlatformSetGammaRamp(monitor, ramp);
    541 }
    542