aboutsummaryrefslogblamecommitdiffstats
path: root/libgamma_CRTC.c
blob: 284beeebb2a831c51511326c70969b6557d63835 (plain) (tree)
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414





























































































































































































































































































































































































































                                                                                                             
/* See LICENSE file for copyright and license details. */
#include "libgamma_CRTC.h"
#include <errno.h>
#include <stdlib.h>

#include <libgamma.h>


/**
 * Make a failure-return
 * 
 * @param   error_code  The error code returned from the failing function or zero to read `errno`
 * @return              The object to return
 */
static jlongArray
fail(JNIEnv *env, int error_code)
{
	jlongArray rc = (*env)->NewLongArray(env, 2);
	jlong e, z = 0;
	if ((error_code == LIBGAMMA_ERRNO_SET) || !error_code)
		error_code = errno;
	e = (jlong)error_code;
	(*env)->SetLongArrayRegion(env, rc, 0, 1, &z);
	(*env)->SetLongArrayRegion(env, rc, 1, 1, &e);
	return rc;
}

/**
 * Make a success-return
 * 
 * @param   state  The native object
 * @return         The object to return
 */
static jlongArray
ok(JNIEnv *env, void *state)
{
	jlong a = (jlong)(size_t)state, z = 0;
	jlongArray rc = (*env)->NewLongArray(env, 2);
	(*env)->SetLongArrayRegion(env, rc, 0, 1, &a);
	(*env)->SetLongArrayRegion(env, rc, 1, 1, &z);
	return rc;
}

/**
 * Create a CRTC state
 * 
 * @param   partition  The partition state for the partition that the CRTC belongs to
 * @param   crtc       The index of the CRTC within the partition
 * @return             Element 0: The value for {@link #address}
 *                     Element 1: Error code, zero on success
 */
jlongArray
Java_libgamma_CRTC_libgamma_1crtc_1create(JNIEnv *env, jclass class, jlong partition, jint crtc)
{
	libgamma_crtc_state_t *state = malloc(sizeof(libgamma_crtc_state_t));
	void *super = (void *)(size_t)partition;
	int r;
	if (!state)
		return fail(env, 0);
	r = libgamma_crtc_initialise(state, super, crtc);
	if (r)
		return fail(env, r);
	return ok(env, state);
	(void) class;
}

/**
 * Release all resources held by a CRTC state
 * and free the CRTC state pointer
 * 
 * @param  address  The CRTC state
 */
void
Java_libgamma_CRTC_libgamma_1crtc_1free(JNIEnv *env, jclass class, jlong address)
{
	void *this = (void *)(size_t)address;
	libgamma_crtc_free(this);
	(void) env;
	(void) class;
}

/**
 * Restore the gamma ramps for a CRTC to the system settings for that CRTC
 * 
 * @param   address  The CRTC state
 * @return           Zero on success, and error code on failure
 */
jint
Java_libgamma_CRTC_libgamma_1crtc_1restore(JNIEnv *env, jclass class, jlong address)
{
	void *this = (void *)(size_t)address;
	int r = libgamma_crtc_restore(this);
	if (r)
		return r == LIBGAMMA_ERRNO_SET ? errno : r;
	return 0;
	(void) env;
	(void) class;
}


/**
 * Read information about a CRTC
 * 
 * @param   crtc    The state of the CRTC whose information should be read
 * @param   fields  OR:ed identifiers for the information about the CRTC that should be read
 * @return          Input parameters for the constructor of {@link CRTCInformation}
 */
jobjectArray
Java_libgamma_CRTC_libgamma_1get_1crtc_1information(JNIEnv *env, jclass class, jlong crtc, jint fields)
{
	void *this_voidp = (void *)(size_t)crtc;
	libgamma_crtc_state_t *this = this_voidp;
	jbyteArray edid = NULL;
	jstring connector_name = NULL;
	jclass class_of_jobject = (*env)->FindClass(env, "java/lang/Object");
	jintArray ints_ = (*env)->NewIntArray(env, 25);
	jfloatArray gamma_ = (*env)->NewFloatArray(env, 3);
	jobjectArray rc = (*env)->NewObjectArray(env, 4, class_of_jobject, NULL);
	libgamma_crtc_information_t info;
	jint ints[25];
	jfloat gamma[3];

	libgamma_get_crtc_information(&info, this, fields);

	if (info.edid) {
		edid = (*env)->NewByteArray(env, info.edid_length);
		(*env)->SetByteArrayRegion(env, edid, 0, info.edid_length, (const jbyte*)(info.edid));
		free(info.edid);
	}

	if (connector_name)
		connector_name = (*env)->NewStringUTF(env, info.connector_name);

	gamma[0] = (jfloat)info.gamma_red;
	gamma[1] = (jfloat)info.gamma_green;
	gamma[2] = (jfloat)info.gamma_blue;

	ints[0] = (jint)info.edid_error;
	ints[1] = (jint)info.width_mm;
	ints[2] = (jint)info.width_mm_error;
	ints[3] = (jint)info.height_mm;
	ints[4] = (jint)info.height_mm_error;
	ints[5] = (jint)info.width_mm_edid;
	ints[6] = (jint)info.width_mm_edid_error;
	ints[7] = (jint)info.height_mm_edid;
	ints[8] = (jint)info.height_mm_edid_error;
	ints[9] = (jint)info.red_gamma_size;
	ints[10] = (jint)info.green_gamma_size;
	ints[11] = (jint)info.blue_gamma_size;
	ints[12] = (jint)info.gamma_size_error;
	ints[13] = (jint)info.gamma_depth;
	ints[14] = (jint)info.gamma_depth_error;
	ints[15] = (jint)info.gamma_support;
	ints[16] = (jint)info.gamma_support_error;
	ints[17] = (jint)info.subpixel_order;
	ints[18] = (jint)info.subpixel_order_error;
	ints[19] = (jint)info.active;
	ints[20] = (jint)info.active_error;
	ints[21] = (jint)info.connector_name_error;
	ints[22] = (jint)info.connector_type;
	ints[23] = (jint)info.connector_type_error;
	ints[24] = (jint)info.gamma_error;

	(*env)->SetIntArrayRegion(env, ints_, 0, 25, ints);
	(*env)->SetFloatArrayRegion(env, gamma_, 0, 3, gamma);

	(*env)->SetObjectArrayElement(env, rc, 0, edid);
	(*env)->SetObjectArrayElement(env, rc, 1, connector_name);
	(*env)->SetObjectArrayElement(env, rc, 2, gamma_);
	(*env)->SetObjectArrayElement(env, rc, 3, ints_);

	return rc;
	(void) class;
}

/**
 * Get the current gamma ramps for a CRTC, 8-bit gamma-depth version
 * 
 * @param   address  The CRTC state
 * @param   ramps    The gamma ramps to fill with the current values
 * @return           Zero on success, an error code on failure
 */
jint
Java_libgamma_CRTC_libgamma_1crtc_1get_1gamma_1ramps8(JNIEnv *env, jclass class, jlong address, jlong ramps)
{
	void *crtc_voidp = (void *)(size_t)address;
	libgamma_crtc_state_t *crtc = crtc_voidp;
	void *output_voidp = (void *)(size_t)ramps;
	libgamma_gamma_ramps8_t *output = output_voidp;
	int r = libgamma_crtc_get_gamma_ramps8(crtc, output);
	return r == LIBGAMMA_ERRNO_SET ? errno : r;
	(void) env;
	(void) class;
}

/**
 * Set the gamma ramps for a CRTC, 8-bit gamma-depth version
 * 
 * @param   address  The CRTC state
 * @param   ramps    The gamma ramps to apply
 * @return           Zero on success, an error code on failure
 */
jint
Java_libgamma_CRTC_libgamma_1crtc_1set_1gamma_1ramps8(JNIEnv *env, jclass class, jlong address, jlong ramps)
{
	void *crtc_voidp = (void *)(size_t)address;
	libgamma_crtc_state_t *crtc = crtc_voidp;
	void *values_voidp = (void *)(size_t)ramps;
	libgamma_gamma_ramps8_t *values = values_voidp;
	int r = libgamma_crtc_set_gamma_ramps8(crtc, *values);
	return r == LIBGAMMA_ERRNO_SET ? errno : r;
	(void) env;
	(void) class;
}

/**
 * Get the current gamma ramps for a CRTC, 16-bit gamma-depth version
 * 
 * @param   address  The CRTC state
 * @param   ramps    The gamma ramps to fill with the current values
 * @return           Zero on success, an error code on failure
 */
jint
Java_libgamma_CRTC_libgamma_1crtc_1get_1gamma_1ramps16(JNIEnv *env, jclass class, jlong address, jlong ramps)
{
	void *crtc_voidp = (void *)(size_t)address;
	libgamma_crtc_state_t *crtc = crtc_voidp;
	void *output_voidp = (void *)(size_t)ramps;
	libgamma_gamma_ramps16_t *output = output_voidp;
	int r = libgamma_crtc_get_gamma_ramps16(crtc, output);
	return r == LIBGAMMA_ERRNO_SET ? errno : r;
	(void) env;
	(void) class;
}

/**
 * Set the gamma ramps for a CRTC, 16-bit gamma-depth version
 * 
 * @param   address  The CRTC state
 * @param   ramps    The gamma ramps to apply
 * @return           Zero on success, an error code on failure
 */
jint
Java_libgamma_CRTC_libgamma_1crtc_1set_1gamma_1ramps16(JNIEnv *env, jclass class, jlong address, jlong ramps)
{
	void *crtc_voidp = (void *)(size_t)address;
	libgamma_crtc_state_t *crtc = crtc_voidp;
	void *values_voidp = (void *)(size_t)ramps;
	libgamma_gamma_ramps16_t *values = values_voidp;
	int r = libgamma_crtc_set_gamma_ramps16(crtc, *values);
	return r == LIBGAMMA_ERRNO_SET ? errno : r;
	(void) env;
	(void) class;
}

/**
 * Get the current gamma ramps for a CRTC, 32-bit gamma-depth version
 * 
 * @param   address  The CRTC state
 * @param   ramps    The gamma ramps to fill with the current values
 * @return           Zero on success, an error code on failure
 */
jint
Java_libgamma_CRTC_libgamma_1crtc_1get_1gamma_1ramps32(JNIEnv *env, jclass class, jlong address, jlong ramps)
{
	void *crtc_voidp = (void *)(size_t)address;
	libgamma_crtc_state_t *crtc = crtc_voidp;
	void *output_voidp = (void *)(size_t)ramps;
	libgamma_gamma_ramps32_t *output = output_voidp;
	int r = libgamma_crtc_get_gamma_ramps32(crtc, output);
	return r == LIBGAMMA_ERRNO_SET ? errno : r;
	(void) env;
	(void) class;
}

/**
 * Set the gamma ramps for a CRTC, 32-bit gamma-depth version
 * 
 * @param   address  The CRTC state
 * @param   ramps    The gamma ramps to apply
 * @return           Zero on success, an error code on failure
 */
jint
Java_libgamma_CRTC_libgamma_1crtc_1set_1gamma_1ramps32(JNIEnv *env, jclass class, jlong address, jlong ramps)
{
	void *crtc_voidp = (void *)(size_t)address;
	libgamma_crtc_state_t *crtc = crtc_voidp;
	void *values_voidp = (void *)(size_t)ramps;
	libgamma_gamma_ramps32_t *values = values_voidp;
	int r = libgamma_crtc_set_gamma_ramps32(crtc, *values);
	return r == LIBGAMMA_ERRNO_SET ? errno : r;
	(void) env;
	(void) class;
}

/**
 * Get the current gamma ramps for a CRTC, 64-bit gamma-depth version
 * 
 * @param   address  The CRTC state
 * @param   ramps    The gamma ramps to fill with the current values
 * @return           Zero on success, an error code on failure
 */
jint
Java_libgamma_CRTC_libgamma_1crtc_1get_1gamma_1ramps64(JNIEnv *env, jclass class, jlong address, jlong ramps)
{
	void *crtc_voidp = (void *)(size_t)address;
	libgamma_crtc_state_t *crtc = crtc_voidp;
	void *output_voidp = (void *)(size_t)ramps;
	libgamma_gamma_ramps64_t *output = output_voidp;
	int r = libgamma_crtc_get_gamma_ramps64(crtc, output);
	return r == LIBGAMMA_ERRNO_SET ? errno : r;
	(void) env;
	(void) class;
}

/**
 * Set the gamma ramps for a CRTC, 64-bit gamma-depth version
 * 
 * @param   address  The CRTC state
 * @param   ramps    The gamma ramps to apply
 * @return           Zero on success, an error code on failure
 */
jint
Java_libgamma_CRTC_libgamma_1crtc_1set_1gamma_1ramps64(JNIEnv *env, jclass class, jlong address, jlong ramps)
{
	void *crtc_voidp = (void *)(size_t)address;
	libgamma_crtc_state_t *crtc = crtc_voidp;
	void *values_voidp = (void *)(size_t)ramps;
	libgamma_gamma_ramps64_t *values = values_voidp;
	int r = libgamma_crtc_set_gamma_ramps64(crtc, *values);
	return r == LIBGAMMA_ERRNO_SET ? errno : r;
	(void) env;
	(void) class;
}

/**
 * Set the gamma ramps for a CRTC, single precision floating point version
 * 
 * @param   address  The CRTC state
 * @param   ramps    The gamma ramps to apply
 * @return           Zero on success, an error code on failure
 */
jint
Java_libgamma_CRTC_libgamma_1crtc_1set_1gamma_1rampsf(JNIEnv *env, jclass class, jlong address, jlong ramps)
{
	void *crtc_voidp = (void *)(size_t)address;
	libgamma_crtc_state_t *crtc = crtc_voidp;
	void *output_voidp = (void *)(size_t)ramps;
	libgamma_gamma_rampsf_t *output = output_voidp;
	int r = libgamma_crtc_get_gamma_rampsf(crtc, output);
	return r == LIBGAMMA_ERRNO_SET ? errno : r;
	(void) env;
	(void) class;
}

/**
 * Get the current gamma ramps for a CRTC, single precision floating point version
 * 
 * @param   address  The CRTC state
 * @param   ramps    The gamma ramps to fill with the current values
 * @return           Zero on success, an error code on failure
 */
jint
Java_libgamma_CRTC_libgamma_1crtc_1get_1gamma_1rampsf(JNIEnv *env, jclass class, jlong address, jlong ramps)
{
	void *crtc_voidp = (void *)(size_t)address;
	libgamma_crtc_state_t * crtc = crtc_voidp;
	void *values_voidp = (void *)(size_t)ramps;
	libgamma_gamma_rampsf_t * values = values_voidp;
	int r = libgamma_crtc_set_gamma_rampsf(crtc, *values);
	return r == LIBGAMMA_ERRNO_SET ? errno : r;
	(void) env;
	(void) class;
}

/**
 * Get the current gamma ramps for a CRTC, double precision floating point version
 * 
 * @param   address  The CRTC state
 * @param   ramps    The gamma ramps to fill with the current values
 * @return           Zero on success, an error code on failure
 */
jint
Java_libgamma_CRTC_libgamma_1crtc_1get_1gamma_1rampsd(JNIEnv *env, jclass class, jlong address, jlong ramps)
{
	void *crtc_voidp = (void *)(size_t)address;
	libgamma_crtc_state_t *crtc = crtc_voidp;
	void *output_voidp = (void *)(size_t)ramps;
	libgamma_gamma_rampsd_t *output = output_voidp;
	int r = libgamma_crtc_get_gamma_rampsd(crtc, output);
	return r == LIBGAMMA_ERRNO_SET ? errno : r;
	(void) env;
	(void) class;
}

/**
 * Set the gamma ramps for a CRTC, double precision floating point version
 * 
 * @param   address  The CRTC state
 * @param   ramps    The gamma ramps to apply
 * @return           Zero on success, an error code on failure
 */
jint
Java_libgamma_CRTC_libgamma_1crtc_1set_1gamma_1rampsd(JNIEnv *env, jclass class, jlong address, jlong ramps)
{
	void *crtc_voidp = (void *)(size_t)address;
	libgamma_crtc_state_t *crtc = crtc_voidp;
	void *values_voidp = (void *)(size_t)ramps;
	libgamma_gamma_rampsd_t *values = values_voidp;
	int r = libgamma_crtc_set_gamma_rampsd(crtc, *values);
	return r == LIBGAMMA_ERRNO_SET ? errno : r;
	(void) env;
	(void) class;
}