aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/fake-w32-gdi.c
blob: f151186e2fcee91f7b7c97b67f1e348acfe3359f (plain) (blame)
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
/**
 * libgamma -- Display server abstraction layer for gamma ramp adjustments
 * Copyright (C) 2014  Mattias Andrée (maandree@member.fsf.org)
 * 
 * This library 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 3 of the License, or
 * (at your option) any later version.
 * 
 * This library 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 this library.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef HAVE_LIBGAMMA_METHOD_W32_GDI
# error Compiling fake-w32-gdi.c without FAKE_LIBGAMMA_METHOD_W32_GDI
#endif

/* This file very sloppily translates Windows GDI calls to X RandR calls.
 * It should by no means be used, without additional modification, as a
 * part of a compatibility layer. The purpose of this file is only to make
 * it possible to test for logical errors in Windows specific code on
 * a GNU/Linux system under X. */


#include "fake-w32-gdi.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#ifndef HAVE_LIBGAMMA_METHOD_X_RANDR
/* Use dummy translation. */


/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd144871(v=vs.85).aspx */
HDC GetDC(HWND hWnd)
{
  /* Just a non-NULL value. */
  (void) hWnd;
  return (HDC*)16;
}

/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd162920(v=vs.85).aspx */
int ReleaseDC(HWND hWnd, HDC hDC)
{
  /* Always successful. */
  (void) hWnd;
  (void) hDC;
  return 1;
}


/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877(v=vs.85).aspx */
int GetDeviceCaps(HDC hDC, int nIndex)
{
  /* We have gamma ramps if the user asks for colour management capabilities. */
  (void) hDC;
  return CM_GAMMA_RAMP + nIndex - COLORMGMTCAPS;
}

/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd372194(v=vs.85).aspx */
BOOL SetDeviceGammaRamp(HDC hDC, LPVOID restrict lpRamp)
{
  /* Always successful. */
  (void) hDC;
  (void) lpRamp;
  return TRUE;
}

/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd316946(v=vs.85).aspx */
BOOL GetDeviceGammaRamp(HDC hDC, LPVOID restrict lpRamp)
{
  /* Always successful. */
  (void) hDC;
  (void) lpRamp;
  return TRUE;
}


/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd183490(v=vs.85).aspx */
HDC CreateDC(LPCTSTR restrict lpszDriver, LPCTSTR restrict lpszDevice,
	     void* restrict lpszOutput, void* restrict lpInitData)
{
  /* `NULL` if not asking for a CRTC, otherwise a non-NULL value. */
  (void) lpszOutput;
  (void) lpInitData;
  (void) lpszDevice;
  return strcmp(lpszDriver, "DISPLAY") ? NULL : (HDC*)16;
}


/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd162609(v=vs.85).aspx */
BOOL EnumDisplayDevices(LPCTSTR lpDevice, restrict DWORD iDevNum,
			PDISPLAY_DEVICE restrict lpDisplayDevice, DWORD dwFlags)
{
  (void) dwFlags;
  /* Check correctness of `lpDevice`. */
  if (lpDevice != NULL)
    {
      fprintf(stderr, "lpDevice (argument 1) for EnumDisplayDevices should be NULL\n");
      abort();
      return FALSE;
    }
  /* Pretend that we have two CRTC:s. */
  if (iDevNum >= 2)
    return FALSE;
  /* Check correctness of `lpDisplayDevice`. */
  if (lpDisplayDevice->cb != sizeof(DISPLAY_DEVICE))
    {
      fprintf(stderr, "lpDisplayDevice->cb for EnumDisplayDevices is not sizeof(DISPLAY_DEVICE)\n");
      abort();
      return FALSE;
    }
  /* Store an arbitrary name for the monitor. */
  strcmp(lpDisplayDevice->DeviceName, "some monitor");
  /* The connector is always enabled. */
  lpDisplayDevice->StateFlags = DISPLAY_DEVICE_ACTIVE;
  return TRUE;
}


#else
/* Use translation to X RandR. */


#include <xcb/xcb.h>
#include <xcb/randr.h>


/**
 * The gamma ramp size that devices will
 * always have in Windows GDI.
 */
#define GAMMA_RAMP_SIZE  256


/**
 * Connection to the X RandR display.
 */
static xcb_connection_t* restrict connection = NULL;

/**
 * Resouces for the screen.
 * We only have one screen, again this is a very sloppy compatibility layer.
 */
static xcb_randr_get_screen_resources_current_reply_t* restrict res_reply = NULL;

/**
 * The number of available CRTC:s, -1 if not known yet.
 */
static ssize_t crtc_count = -1;

/**
 * List of X RandR CRTC:s.
 */
static xcb_randr_crtc_t* restrict crtcs = NULL;

/**
 * The number of opened CRTC:s.
 */
static size_t dc_count = 0;



/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd144871(v=vs.85).aspx */
HDC GetDC(HWND hWnd)
{
  /* Return the primary CRTC. */
  (void) hWnd;
  return CreateDC(TEXT("DISPLAY"), "0", NULL, NULL);
}


/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd162920(v=vs.85).aspx */
int ReleaseDC(HWND hWnd, HDC hDC)
{
  /* Disconnect from the RandR display when all monitors have been closed. */
  (void) hWnd;
  (void) hDC;
  dc_count--;
  if (dc_count == 0)
    {
      if (connection != NULL)
	xcb_disconnect(connection);
      connection = NULL;
      free(res_reply);
      res_reply = NULL;
    }
  return 1;
}


/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877(v=vs.85).aspx */
int GetDeviceCaps(HDC hDC, int nIndex)
{
  /* We have gamma ramps if the user asks for colour management capabilities. */
  (void) hDC;
  return CM_GAMMA_RAMP + nIndex - COLORMGMTCAPS;
}


/* xcb violates the rule to never return struct:s. */
#ifdef __GCC__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Waggregate-return"
#endif


/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd372194(v=vs.85).aspx */
BOOL SetDeviceGammaRamp(HDC hDC, LPVOID restrict lpRamp)
{
  /* We assume that our gamma ramps are of the same size
     as used by Windows GDI (we are so sloppy.) */
  xcb_void_cookie_t gamma_cookie =
    xcb_randr_set_crtc_gamma_checked(connection, *(xcb_randr_crtc_t*)hDC, GAMMA_RAMP_SIZE,
				     ((uint16_t*)lpRamp) + 0 * GAMMA_RAMP_SIZE,
				     ((uint16_t*)lpRamp) + 1 * GAMMA_RAMP_SIZE,
				     ((uint16_t*)lpRamp) + 2 * GAMMA_RAMP_SIZE);
  xcb_generic_error_t* error = xcb_request_check(connection, gamma_cookie);
  return error == NULL ? TRUE : FALSE;
}


/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd316946(v=vs.85).aspx */
BOOL GetDeviceGammaRamp(HDC hDC, LPVOID restrict lpRamp)
{
  xcb_randr_get_crtc_gamma_cookie_t gamma_cookie;
  xcb_randr_get_crtc_gamma_reply_t* restrict gamma_reply;
  xcb_generic_error_t* error;
  
  /* Read current gamma ramps. */
  gamma_cookie = xcb_randr_get_crtc_gamma(connection, *(xcb_randr_crtc_t*)hDC);
  gamma_reply = xcb_randr_get_crtc_gamma_reply(connection, gamma_cookie, &error);
  if (error)
    return FALSE;
  
#define DEST_RAMP(I)  (((uint16_t*)lpRamp) + (I) * GAMMA_RAMP_SIZE)
#define SRC_RAMP(C)  (xcb_randr_get_crtc_gamma_##C(gamma_reply))
  
  /* Copy the ramps into the output parameter. (coalesced) */
  memcpy(DEST_RAMP(0), SRC_RAMP(red),   GAMMA_RAMP_SIZE * sizeof(uint16_t));
  memcpy(DEST_RAMP(1), SRC_RAMP(green), GAMMA_RAMP_SIZE * sizeof(uint16_t));
  memcpy(DEST_RAMP(2), SRC_RAMP(blue),  GAMMA_RAMP_SIZE * sizeof(uint16_t));
  
#undef SRC_RAMP
#undef DEST_RAMP
  
  free(gamma_reply);
  return TRUE;
}


/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd183490(v=vs.85).aspx */
HDC CreateDC(LPCTSTR restrict lpszDriver, LPCTSTR restrict lpszDevice,
	     void* restrict lpszOutput, void* restrict lpInitData)
{
  int crtc_index = atoi(lpszDevice);
  
  (void) lpszOutput;
  (void) lpInitData;
  
  /* Check correctness of input. */
  if (strcmp(lpszDriver, "DISPLAY"))
    return NULL;
  
  /* Connect to the display and get screen data if not already done so. */
  if (dc_count == 0)
    {
      xcb_generic_error_t* error;
      xcb_screen_iterator_t iter;
      xcb_randr_get_screen_resources_current_cookie_t res_cookie;
      
      /* Connect to the display. */
      connection = xcb_connect(NULL, NULL);
      /* Get the first screen. */
      iter = xcb_setup_roots_iterator(xcb_get_setup(connection));
      /* Get the resources of the screen. */
      res_cookie = xcb_randr_get_screen_resources_current(connection, iter.data->root);
      res_reply = xcb_randr_get_screen_resources_current_reply(connection, res_cookie, &error);
      if (error)
	{
	  fprintf(stderr, "Failed to open X connection.\n");
	  xcb_disconnect(connection);
	  crtc_count = -1;
	  return NULL;
	}
      
      /* Get the number of CRTC:s. */
      crtc_count = res_reply->num_crtcs;
      /* Get the CRTC ID:s. */
      crtcs = xcb_randr_get_screen_resources_current_crtcs(res_reply);
    }
  
  /* Was the index too high. */
  if (crtc_index >= crtc_count)
    {
      /* Disconnect and release resouces and
	 mark that we do not know the number of
	 available CRTC:s if we have not opened
	 any monitors yet. */
      if (dc_count == 0)
	{
	  xcb_disconnect(connection);
	  free(res_reply);
	  res_reply = NULL;
	  crtc_count = -1;
	}
      return NULL;
    }
  
  /* We have opened a new CRTC. */
  dc_count++;
  /* Return the ID of the CRTC. */
  return crtcs + crtc_index;
}


#ifdef __GCC__
# pragma GCC diagnostic pop
#endif


/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd162609(v=vs.85).aspx */
BOOL EnumDisplayDevices(LPCTSTR restrict lpDevice, DWORD iDevNum,
			PDISPLAY_DEVICE restrict lpDisplayDevice, DWORD dwFlags)
{
  size_t count = (size_t)crtc_count;
  (void) dwFlags;
  /* Check the correctness of the input. */
  if (lpDevice != NULL)
    {
      fprintf(stderr, "lpDevice (argument 1) for EnumDisplayDevices should be NULL\n");
      abort();
      return FALSE;
    }
  /* Do we know how many CRTC:s are available? */
  if (crtc_count < 0)
    {
      /* If not open the first CRTC so that will be figured out. */
      if (GetDC(NULL) == NULL)
	return FALSE;
      count = (size_t)crtc_count;
      /* Close the primary monitor that we just closed. */
      ReleaseDC(NULL, NULL);
    }
  /* Check that the request CRTC exists. */
  if (iDevNum >= count)
    return FALSE;
  /* Check the correctness of the input. */
  if (lpDisplayDevice->cb != sizeof(DISPLAY_DEVICE))
    {
      fprintf(stderr, "lpDisplayDevice->cb for EnumDisplayDevices is not sizeof(DISPLAY_DEVICE)\n");
      abort();
      return FALSE;
    }
  /* Store name for the CRTC. */
  sprintf(lpDisplayDevice->DeviceName, "%i", iDevNum);
  /* The connector that the CRTC belongs to is enabled. */
  lpDisplayDevice->StateFlags = DISPLAY_DEVICE_ACTIVE;
  return TRUE;
}


#endif