aboutsummaryrefslogtreecommitdiffstats
path: root/code/macosx/macosx_display.m
blob: 52235a5dd908e0f73b18804827b37dc43fd1e816 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.

This file is part of Quake III Arena source code.

Quake III Arena source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

Quake III Arena source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/

#import "macosx_display.h"

#include "tr_local.h"
#import "macosx_local.h"

#import <Foundation/Foundation.h>
#import <IOKit/graphics/IOGraphicsTypes.h>  // for interpreting the kCGDisplayIOFlags element of the display mode


NSDictionary *Sys_GetMatchingDisplayMode(qboolean allowStretchedModes)
{
    NSArray *displayModes;
    NSDictionary *mode;
    unsigned int modeIndex, modeCount, bestModeIndex;
    int verbose;
    cvar_t *cMinFreq, *cMaxFreq;
    int minFreq, maxFreq;
    unsigned int colorDepth;
    
    verbose = r_verbose->integer;

    colorDepth = r_colorbits->integer;
    if (colorDepth < 16 || !r_fullscreen->integer)
        colorDepth = [[glw_state.desktopMode objectForKey: (id)kCGDisplayBitsPerPixel] intValue];

    cMinFreq = ri.Cvar_Get("r_minDisplayRefresh", "0", CVAR_ARCHIVE);
    cMaxFreq = ri.Cvar_Get("r_maxDisplayRefresh", "0", CVAR_ARCHIVE);

    if (cMinFreq && cMaxFreq && cMinFreq->integer && cMaxFreq->integer &&
        cMinFreq->integer > cMaxFreq->integer) {
        ri.Error(ERR_FATAL, "r_minDisplayRefresh must be less than or equal to r_maxDisplayRefresh");
    }

    minFreq = cMinFreq ? cMinFreq->integer : 0;
    maxFreq = cMaxFreq ? cMaxFreq->integer : 0;
    
    displayModes = (NSArray *)CGDisplayAvailableModes(glw_state.display);
    if (!displayModes) {
        ri.Error(ERR_FATAL, "CGDisplayAvailableModes returned NULL -- 0x%0x is an invalid display", glw_state.display);
    }
    
    modeCount = [displayModes count];
    if (verbose) {
        ri.Printf(PRINT_ALL, "%d modes avaliable\n", modeCount);
        ri.Printf(PRINT_ALL, "Current mode is %s\n", [[(id)CGDisplayCurrentMode(glw_state.display) description] cString]);
    }
    
    // Default to the current desktop mode
    bestModeIndex = 0xFFFFFFFF;
    
    for ( modeIndex = 0; modeIndex < modeCount; ++modeIndex ) {
        id object;
        int refresh;
        
        mode = [displayModes objectAtIndex: modeIndex];
        if (verbose) {
            ri.Printf(PRINT_ALL, " mode %d -- %s\n", modeIndex, [[mode description] cString]);
        }

        // Make sure we get the right size
        object = [mode objectForKey: (id)kCGDisplayWidth];

        if ([[mode objectForKey: (id)kCGDisplayWidth] intValue] != glConfig.vidWidth ||
            [[mode objectForKey: (id)kCGDisplayHeight] intValue] != glConfig.vidHeight) {
            if (verbose)
                ri.Printf(PRINT_ALL, " -- bad size\n");
            continue;
        }

        if (!allowStretchedModes) {
            if ([[mode objectForKey: (id)kCGDisplayIOFlags] intValue] & kDisplayModeStretchedFlag) {
                if (verbose)
                    ri.Printf(PRINT_ALL, " -- stretched modes disallowed\n");
                continue;
            }
        }

        // Make sure that our frequency restrictions are observed
        refresh = [[mode objectForKey: (id)kCGDisplayRefreshRate] intValue];
        if (minFreq &&  refresh < minFreq) {
            if (verbose)
                ri.Printf(PRINT_ALL, " -- refresh too low\n");
            continue;
        }

        if (maxFreq && refresh > maxFreq) {
            if (verbose)
                ri.Printf(PRINT_ALL, " -- refresh too high\n");
            continue;
        }

        if ([[mode objectForKey: (id)kCGDisplayBitsPerPixel] intValue] != colorDepth) {
            if (verbose)
                ri.Printf(PRINT_ALL, " -- bad depth\n");
            continue;
        }

        bestModeIndex = modeIndex;
        if (verbose)
            ri.Printf(PRINT_ALL, " -- OK\n", bestModeIndex);
    }

    if (verbose)
        ri.Printf(PRINT_ALL, " bestModeIndex = %d\n", bestModeIndex);

    if (bestModeIndex == 0xFFFFFFFF) {
        ri.Printf(PRINT_ALL, "No suitable display mode available.\n");
        return nil;
    }
    
    return [displayModes objectAtIndex: bestModeIndex];
}


#define MAX_DISPLAYS 128

void Sys_GetGammaTable(glwgamma_t *table)
{
    CGTableCount tableSize = 512;
    CGDisplayErr err;
    
    table->tableSize = tableSize;
    if (table->red)
        free(table->red);
    table->red = malloc(tableSize * sizeof(*table->red));
    if (table->green)
        free(table->green);
    table->green = malloc(tableSize * sizeof(*table->green));
    if (table->blue)
        free(table->blue);
    table->blue = malloc(tableSize * sizeof(*table->blue));
    
    // TJW: We _could_ loop here if we get back the same size as our table, increasing the table size.
    err = CGGetDisplayTransferByTable(table->display, tableSize, table->red, table->green, table->blue,
&table->tableSize);
    if (err != CGDisplayNoErr) {
        Com_Printf("GLimp_Init: CGGetDisplayTransferByTable returned %d.\n", err);
        table->tableSize = 0;
    }
}

void Sys_SetGammaTable(glwgamma_t *table)
{
}


void Sys_StoreGammaTables()
{
    // Store the original gamma for all monitors so that we can fade and unfade them all
    CGDirectDisplayID displays[MAX_DISPLAYS];
    CGDisplayCount displayIndex;
    CGDisplayErr err;

    err = CGGetActiveDisplayList(MAX_DISPLAYS, displays, &glw_state.displayCount);
    if (err != CGDisplayNoErr)
        Sys_Error("Cannot get display list -- CGGetActiveDisplayList returned %d.\n", err);
    
    glw_state.originalDisplayGammaTables = calloc(glw_state.displayCount, sizeof(*glw_state.originalDisplayGammaTables));
    for (displayIndex = 0; displayIndex < glw_state.displayCount; displayIndex++) {
        glwgamma_t *table;

        table = &glw_state.originalDisplayGammaTables[displayIndex];
        table->display = displays[displayIndex];
        Sys_GetGammaTable(table);
    }
}


//  This isn't a mathematically correct fade, but we don't care that much.
void Sys_SetScreenFade(glwgamma_t *table, float fraction)
{
    CGTableCount tableSize;
    CGGammaValue *red, *blue, *green;
    CGTableCount gammaIndex;
    
    if (!glConfig.deviceSupportsGamma)
        return;

    if (!(tableSize = table->tableSize))
        // we couldn't get the table for this display for some reason
        return;
    
//    Com_Printf("0x%08x %f\n", table->display, fraction);
    
    red = glw_state.tempTable.red;
    green = glw_state.tempTable.green;
    blue = glw_state.tempTable.blue;
    if (glw_state.tempTable.tableSize < tableSize) {
        glw_state.tempTable.tableSize = tableSize;
        red = realloc(red, sizeof(*red) * tableSize);
        green = realloc(green, sizeof(*green) * tableSize);
        blue = realloc(blue, sizeof(*blue) * tableSize);
        glw_state.tempTable.red = red;
        glw_state.tempTable.green = green;
        glw_state.tempTable.blue = blue;
    }

    for (gammaIndex = 0; gammaIndex < table->tableSize; gammaIndex++) {
        red[gammaIndex] = table->red[gammaIndex] * fraction;
        blue[gammaIndex] = table->blue[gammaIndex] * fraction;
        green[gammaIndex] = table->green[gammaIndex] * fraction;
    }
    
    CGSetDisplayTransferByTable(table->display, table->tableSize, red, green, blue);
}

// Fades all the active displays at the same time.

#define FADE_DURATION 0.5
void Sys_FadeScreens()
{
    CGDisplayCount displayIndex;
    int stepIndex;
    glwgamma_t *table;
    NSTimeInterval start, current;
    float time;
    
    if (!glConfig.deviceSupportsGamma)
        return;

    Com_Printf("Fading all displays\n");
    
    start = [NSDate timeIntervalSinceReferenceDate];
    time = 0.0;
    while (time != FADE_DURATION) {
        current = [NSDate timeIntervalSinceReferenceDate];
        time = current - start;
        if (time > FADE_DURATION)
            time = FADE_DURATION;
            
        for (displayIndex = 0; displayIndex < glw_state.displayCount; displayIndex++) {            
            table = &glw_state.originalDisplayGammaTables[displayIndex];
            Sys_SetScreenFade(table, 1.0 - time / FADE_DURATION);
        }
    }
}

void Sys_FadeScreen(CGDirectDisplayID display)
{
    CGDisplayCount displayIndex;
    glwgamma_t *table;
    int stepIndex;
    
    if (!glConfig.deviceSupportsGamma)
        return;

    Com_Printf("Fading display 0x%08x\n", display);

    for (displayIndex = 0; displayIndex < glw_state.displayCount; displayIndex++) {
        if (display == glw_state.originalDisplayGammaTables[displayIndex].display) {
            NSTimeInterval start, current;
            float time;
            
            start = [NSDate timeIntervalSinceReferenceDate];
            time = 0.0;

            table = &glw_state.originalDisplayGammaTables[displayIndex];
            while (time != FADE_DURATION) {
                current = [NSDate timeIntervalSinceReferenceDate];
                time = current - start;
                if (time > FADE_DURATION)
                    time = FADE_DURATION;

                Sys_SetScreenFade(table, 1.0 - time / FADE_DURATION);
            }
            return;
        }
    }

    Com_Printf("Unable to find display to fade it\n");
}

void Sys_UnfadeScreens()
{
    CGDisplayCount displayIndex;
    int stepIndex;
    glwgamma_t *table;
    NSTimeInterval start, current;
    float time;
    
    if (!glConfig.deviceSupportsGamma)
        return;
        
    Com_Printf("Unfading all displays\n");

    start = [NSDate timeIntervalSinceReferenceDate];
    time = 0.0;
    while (time != FADE_DURATION) {
        current = [NSDate timeIntervalSinceReferenceDate];
        time = current - start;
        if (time > FADE_DURATION)
            time = FADE_DURATION;
            
        for (displayIndex = 0; displayIndex < glw_state.displayCount; displayIndex++) {            
            table = &glw_state.originalDisplayGammaTables[displayIndex];
            Sys_SetScreenFade(table, time / FADE_DURATION);
        }
    }
}

void Sys_UnfadeScreen(CGDirectDisplayID display, glwgamma_t *table)
{
    CGDisplayCount displayIndex;
    int stepIndex;
    
    if (!glConfig.deviceSupportsGamma)
        return;
    
    Com_Printf("Unfading display 0x%08x\n", display);

    if (table) {
        CGTableCount i;
        
        Com_Printf("Given table:\n");
        for (i = 0; i < table->tableSize; i++) {
            Com_Printf("  %f %f %f\n", table->red[i], table->blue[i], table->green[i]);
        }
    }
    
    // Search for the original gamma table for the display
    if (!table) {
        for (displayIndex = 0; displayIndex < glw_state.displayCount; displayIndex++) {
            if (display == glw_state.originalDisplayGammaTables[displayIndex].display) {
                table = &glw_state.originalDisplayGammaTables[displayIndex];
                break;
            }
        }
    }
    
    if (table) {
        NSTimeInterval start, current;
        float time;
        
        start = [NSDate timeIntervalSinceReferenceDate];
        time = 0.0;

        while (time != FADE_DURATION) {
            current = [NSDate timeIntervalSinceReferenceDate];
            time = current - start;
            if (time > FADE_DURATION)
                time = FADE_DURATION;
            Sys_SetScreenFade(table, time / FADE_DURATION);
        }
        return;
    }
    
    Com_Printf("Unable to find display to unfade it\n");
}