-
Notifications
You must be signed in to change notification settings - Fork 3
/
compicc.c
2601 lines (2173 loc) · 82.5 KB
/
compicc.c
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file compicc.c
*
* @brief a compiz desktop colour management plug-in
*
* @author Kai-Uwe Behrmann, based on Tomas' color filter, based on Gerhard
* Fürnkranz' GLSL ppm_viewer
* @par Copyright:
* 2008 (C) Gerhard Fürnkranz, 2008 (C) Tomas Carnecky,
* 2009-2016 (C) Kai-Uwe Behrmann
* @par License:
* new BSD <http://www.opensource.org/licenses/bsd-license.php>
* @since 2009/02/23
*/
/* strdup needs _BSD_SOURCE */
#define _BSD_SOURCE
#include <assert.h>
#include <math.h> // floor()
#include <string.h> // http://www.opengroup.org/onlinepubs/009695399/functions/strdup.html
#include <sys/time.h>
#include <time.h>
#include <unistd.h> // getpid()
#include <stdarg.h>
#include <icc34.h>
#define GL_GLEXT_PROTOTYPES
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xfixes.h>
#define HAVE_XRANDR
#ifdef HAVE_XRANDR
#include <X11/extensions/Xrandr.h>
#endif
#include <compiz-common.h>
#include <oyranos.h>
#include <oyranos_devices.h>
#include <oyranos_threads.h>
#include <oyConversion_s.h>
#include <oyFilterGraph_s.h>
#include <oyFilterNode_s.h>
#include <oyRectangle_s.h>
#include <X11/Xcm/Xcm.h>
#include <X11/Xcm/XcmEvents.h>
#define OY_COMPIZ_VERSION (COMPIZ_VERSION_MAJOR * 10000 + COMPIZ_VERSION_MINOR * 100 + COMPIZ_VERSION_MICRO)
#if OY_COMPIZ_VERSION < 708
#define oyCompLogMessage(disp_, plug_in_name, debug_level, format_, ... ) \
compLogMessage( disp_, plug_in_name, debug_level, format_, __VA_ARGS__ )
#else
#define oyCompLogMessage( disp_, plug_in_name, debug_level, format_, ... ) \
{ if(oy_debug) \
oyMessageFunc_p( oyMSG_DBG, NULL, format_, __VA_ARGS__); \
else \
compLogMessage( plug_in_name, debug_level, format_, __VA_ARGS__ ); \
}
#endif
#if OY_COMPIZ_VERSION < 900
#include <compiz-core.h>
#else
#define CompBool bool
#endif
#if OYRANOS_VERSION < 201
#define oyConversion_GetGraph( conversion ) oyFilterGraph_FromNode( conversion->input, 0)
#endif
/* Uncomment the following line if you want to enable debugging output */
#define PLUGIN_DEBUG 1
/**
* The 3D lookup texture has 64 points in each dimension, using 16 bit integers.
* That means each active region will use 1.5MiB of texture memory.
*/
#define GRIDPOINTS 64
static signed long colour_desktop_region_count = -1;
/**
* The stencil ID is a property of each window region to identify the used
* bit plane in the stencil buffer.
* Each screen context obtains a different range of IDs (i).
* j is the actual region in the window.
*/
#define STENCIL_ID ( 1 + colour_desktop_region_count * i + pw->stencil_id_start + j )
#define HAS_REGIONS(pw) (pw->nRegions > 1)
#define WINDOW_BORDER 30
#if defined(PLUGIN_DEBUG)
#define DBG printf("%s:%d %s() %.02f\n", DBG_ARGS);
#else
#define DBG
#endif
static int icc_profile_flags = 0;
#define DBG_STRING " %s:%d %s() %.02f "
#define DBG_ARGS (strrchr(__FILE__,'/') ? strrchr(__FILE__,'/')+1 : __FILE__),__LINE__,__func__,(double)clock()/CLOCKS_PER_SEC
#if defined(PLUGIN_DEBUG)
#define START_CLOCK(text) fprintf( stderr, DBG_STRING text " - ", DBG_ARGS );
#define END_CLOCK fprintf( stderr, "%.02f\n", (double)clock()/CLOCKS_PER_SEC );
#else
#define START_CLOCK(text)
#define END_CLOCK
#endif
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
void* oyAllocateFunc_ (size_t size);
void oyDeAllocateFunc_ (void * data);
#ifdef __cplusplus
}
#endif /* __cplusplus */
void* cicc_alloc (size_t size) { void * p = oyAllocateFunc_(size); memset(p,0,size); return p; }
void cicc_free (void * data) { oyDeAllocateFunc_(data); }
typedef CompBool (*dispatchObjectProc) (CompPlugin *plugin, CompObject *object, void *privateData);
/** Be active once and then not again. */
static int colour_desktop_can = 1;
/* last check time */
static time_t icc_color_desktop_last_time = 0;
/**
* All data to create and use a color conversion.
* Included are OpenGL texture, the source ICC profile for reference and the
* target profile for the used monitor.
*/
typedef struct {
oyProfile_s * src_profile; /* the data profile or device link */
oyProfile_s * dst_profile; /* the monitor profile or none */
char * output_name; /* the intented output device */
GLushort clut[GRIDPOINTS][GRIDPOINTS][GRIDPOINTS][3]; /* lookup table */
GLuint glTexture; /* texture reference */
GLfloat scale, offset; /* texture parameters */
int ref; /* reference counter */
} PrivColorContext;
/**
* The XserverRegion is dereferenced when the client sets it on a window.
* This allows clients to change the region as the window is resized.
* The profile is resolved as soon as the client uploads the regions into the
* window. That means clients need to upload profiles first and then the
* regions. Otherwise the plugin won't be able to find the profile and no color
* transformation will be done.
*/
typedef struct {
/* These members are only valid when this region is part of the
* active stack range. */
uint8_t md5[16];
PrivColorContext ** cc;
Region xRegion;
} PrivColorRegion;
/**
* Output profiles are currently only fetched using XRandR. For backwards
* compatibility the code should fall back to root window properties
* (XCM_ICC_V0_3_TARGET_PROFILE_IN_X_BASE).
*/
typedef struct {
char name[32];
PrivColorContext cc;
XRectangle xRect;
} PrivColorOutput;
static CompMetadata pluginMetadata;
static int core_priv_index = -1;
static int display_priv_index = -1;
static int screen_priv_index = -1;
static int window_priv_index = -1;
typedef struct {
int childPrivateIndex;
ObjectAddProc objectAdd;
} PrivCore;
typedef struct {
int childPrivateIndex;
HandleEventProc handleEvent;
/* Window properties */
Atom iccColorProfiles;
Atom iccColorRegions;
Atom iccColorOutputs;
Atom iccColorDesktop;
Atom netDesktopGeometry;
Atom iccDisplayAdvanced;
} PrivDisplay;
typedef struct {
int childPrivateIndex;
/* hooked functions */
DrawWindowProc drawWindow;
DrawWindowTextureProc drawWindowTexture;
/* compiz fragement function */
int function, param, unit;
int function_2, param_2, unit_2;
/* XRandR outputs and the associated profiles */
unsigned long nContexts;
PrivColorOutput *contexts;
} PrivScreen;
typedef struct {
/* start of stencil IDs + nRegions need to be reserved for this window,
* inside each monitors stencil ID range */
unsigned long stencil_id_start;
/* regions attached to the window */
unsigned long nRegions;
PrivColorRegion *pRegion;
/* old absolute region */
oyRectangle_s * absoluteWindowRectangleOld;
/* active stack range */
unsigned long active;
/* active XRandR output */
char *output;
} PrivWindow;
static Region absoluteRegion(CompWindow *w, Region region);
static void damageWindow(CompWindow *w, void *closure);
static void addWindowRegionCount(CompWindow *w, void * count);
oyPointer pluginGetPrivatePointer ( CompObject * o );
static void updateOutputConfiguration( CompScreen * s,
CompBool init,
int screen );
static int updateIccColorDesktopAtom ( CompScreen * s,
PrivScreen * ps,
int request );
static oyPointer getScreenProfile ( CompScreen * s,
int screen,
int server,
size_t * size );
int needUpdate ( Display * display );
static void moveICCprofileAtoms ( CompScreen * s,
int screen,
int init );
void cleanDisplayProfiles ( CompScreen * s );
static int getDisplayAdvanced ( CompScreen * s,
int screen );
static int getDeviceProfile ( CompScreen * s,
PrivScreen * ps,
oyConfig_s * device,
int screen );
oyProfile_s * profileFromMD5 ( uint8_t * md5 );
static void setupOutputTable ( CompScreen * s,
oyConfig_s * device,
int screen );
static void setupColourTable ( PrivColorContext * ccontext,
int advanced,
CompScreen * s );
static void changeProperty ( Display * display,
Atom target_atom,
int type,
void * data,
unsigned long size );
static void *fetchProperty(Display *dpy, Window w, Atom prop, Atom type, unsigned long *n, Bool del);
static oyStructList_s * pluginGetPrivatesCache ();
static void *compObjectGetPrivate(CompObject *o)
{
oyPointer private_data = pluginGetPrivatePointer( o );
return private_data;
}
static void compObjectFreePrivate(CompObject *o)
{
int index = -1;
switch(o->type)
{
case COMP_OBJECT_TYPE_CORE:
index = core_priv_index;
break;
case COMP_OBJECT_TYPE_DISPLAY:
index = display_priv_index;
break;
case COMP_OBJECT_TYPE_SCREEN:
index = screen_priv_index;
break;
case COMP_OBJECT_TYPE_WINDOW:
index = window_priv_index;
break;
}
if(index < 0)
return;
oyPointer ptr = o->privates[index].ptr;
o->privates[index].ptr = NULL;
if(ptr)
cicc_free(ptr);
}
/**
* Xcolor helper functions. I didn't really want to put them into the Xcolor
* library.
* Other window managers are free to copy those when needed.
*/
static inline XcolorProfile *XcolorProfileNext(XcolorProfile *profile)
{
unsigned char *ptr = (unsigned char *) profile;
return (XcolorProfile *) (ptr + sizeof(XcolorProfile) + ntohl(profile->length));
}
static inline unsigned long XcolorProfileCount(void *data, unsigned long nBytes)
{
unsigned long count = 0;
XcolorProfile * ptr;
for (ptr = (XcolorProfile*)data;
(uintptr_t)ptr < (uintptr_t)data + nBytes;
ptr = XcolorProfileNext(ptr))
++count;
return count;
}
static inline XcolorRegion *XcolorRegionNext(XcolorRegion *region)
{
unsigned char *ptr = (unsigned char *) region;
return (XcolorRegion *) (ptr + sizeof(XcolorRegion));
}
static inline unsigned long XcolorRegionCount(void *data OY_UNUSED, unsigned long nBytes)
{
return nBytes / sizeof(XcolorRegion);
}
/**
* Helper function to convert a MD5 into a readable string.
*/
static const char *md5string(const uint8_t md5[16])
{
static char buffer[33] = {0};
const uint32_t * h = (const uint32_t*)md5;
sprintf( buffer, "%x%x%x%x", h[0],h[1],h[2],h[3]);
return buffer;
}
/**
* Here begins the real code
*/
static int getFetchTarget(CompTexture *texture)
{
if (texture->target == GL_TEXTURE_2D) {
return COMP_FETCH_TARGET_2D;
} else {
return COMP_FETCH_TARGET_RECT;
}
}
/**
* The shader is the same for all windows and profiles. It only depends on the
* 3D texture and two environment variables.
*/
static int getProfileShader(CompScreen *s, CompTexture *texture, int param, int unit)
{
PrivScreen *ps = compObjectGetPrivate((CompObject *) s);
int function = -1;
if (ps->function && ps->param == param && ps->unit == unit)
return ps->function;
if(ps->function_2 && ps->param_2 == param && ps->unit_2 == unit)
return ps->function_2;
if( ps->function_2)
destroyFragmentFunction(s, ps->function_2);
/*else if(ps->function) should not happen as funcition is statical cached
destroyFragmentFunction(s, ps->function); */
/* shaders are programmed using ARB GPU assembly language */
CompFunctionData *data = createFunctionData();
addTempHeaderOpToFunctionData(data, "temp");
addFetchOpToFunctionData(data, "output", NULL, getFetchTarget(texture));
/* store alpha */
addDataOpToFunctionData(data, "MOV temp, output;");
/* needed, but why? */
addDataOpToFunctionData(data, "MAD output, output, program.env[%d], program.env[%d];", param, param + 1);
/* colour transform through a texture lookup */
addDataOpToFunctionData(data, "TEX output, output, texture[%d], 3D;", unit);
/* multiply alpha */
addDataOpToFunctionData(data, "MUL output, temp.a, output;");
addColorOpToFunctionData (data, "output", "output");
function = createFragmentFunction(s, "compicc", data);
if(ps->param == -1)
{
ps->function = function;
ps->param = param;
ps->unit = unit;
return ps->function;
} else
{
ps->function_2 = function;
ps->param_2 = param;
ps->unit_2 = unit;
return ps->function_2;
}
}
/**
* Converts a server-side region to a client-side region.
*/
static Region convertRegion(Display *dpy, XserverRegion src)
{
Region ret = XCreateRegion();
int nRects = 0;
XRectangle *rect = XFixesFetchRegion(dpy, src, &nRects);
for (int i = 0; i < nRects; ++i) {
XUnionRectWithRegion(&rect[i], ret, ret);
}
XFree(rect);
return ret;
}
static Region windowRegion( CompWindow * w )
{
Region r = XCreateRegion();
XRectangle rect = {0,0,w->serverWidth, w->serverHeight};
XUnionRectWithRegion( &rect, r, r );
return r;
}
/**
* Generic function to fetch a window property.
*/
static void *fetchProperty(Display *dpy, Window w, Atom prop, Atom type, unsigned long *n, Bool del)
{
Atom actual;
int format;
unsigned long left;
unsigned char *data;
const char * atom_name = XGetAtomName( dpy, prop );
XFlush( dpy );
int result = XGetWindowProperty( dpy, w, prop, 0, ~0, del, type, &actual,
&format, n, &left, &data );
oyCompLogMessage(d, "compicc", CompLogLevelDebug, DBG_STRING "XGetWindowProperty w: %lu atom: %s n: %lu left: %lu", DBG_ARGS, w, atom_name, *n, left );
if(del)
printf( "compicc erasing atom %lu\n", prop );
if (result == Success)
return (void *) data;
return NULL;
}
/**
* Called when new profiles have been attached to the root window. Fetches
* these and saves them in a local database.
*/
static void updateScreenProfiles(CompScreen *s)
{
CompDisplay *d = s->display;
PrivDisplay *pd = compObjectGetPrivate((CompObject *) d);
/* Fetch the profiles */
unsigned long nBytes;
int screen = DefaultScreen( s->display->display );
void *data = fetchProperty(d->display,
XRootWindow( s->display->display, screen ),
pd->iccColorProfiles,
XA_CARDINAL, &nBytes, True);
if (data == NULL)
return;
uint32_t exact_hash_size = 0;
oyHash_s * entry;
oyProfile_s * prof = NULL;
oyStructList_s * cache = pluginGetPrivatesCache();
int n = 0;
/* Grow or shring the array as needed. */
unsigned long count = XcolorProfileCount(data, nBytes);
/* Copy the profiles into the array, and create the Oyranos handles. */
XcolorProfile *profile = data;
for (unsigned long i = 0; i < count; ++i)
{
const char * hash_text = md5string(profile->md5);
entry = oyStructList_GetHash( cache, exact_hash_size, hash_text );
prof = (oyProfile_s *) oyHash_GetPointer( entry, oyOBJECT_PROFILE_S);
/* XcolorProfile::length == 0 means the clients wants to delete the profile. */
if( ntohl(profile->length) )
{
if(!prof)
{
prof = oyProfile_FromMem( htonl(profile->length), profile + 1, 0,NULL );
if(!prof)
{
/* If creating the Oyranos profile fails, don't try to parse any further profiles and just quit. */
oyCompLogMessage(d, "compicc", CompLogLevelWarn, "Couldn't create Oyranos profile %s", hash_text );
goto out;
}
oyHash_SetPointer( entry, (oyStruct_s*) prof );
++n;
}
}
profile = XcolorProfileNext(profile);
}
#if defined(PLUGIN_DEBUG_)
oyCompLogMessage(d, "compicc", CompLogLevelDebug, "Added %d of %d screen profiles",
n, count);
#endif
out:
XFree(data);
}
oyProfile_s * profileFromMD5 ( uint8_t * md5 )
{
uint32_t exact_hash_size = 0;
oyProfile_s * prof = NULL;
oyStructList_s * cache = pluginGetPrivatesCache();
/* Copy the profiles into the array, and create the Oyranos handles. */
const char * hash_text = md5string(md5);
prof = (oyProfile_s *) oyStructList_GetHashStruct( cache, exact_hash_size,
hash_text, oyOBJECT_PROFILE_S );
return prof;
}
/**
* Called when new regions have been attached to a window. Fetches these and
* saves them in the local list.
*/
static void updateWindowRegions(CompWindow *w)
{
PrivWindow *pw = compObjectGetPrivate((CompObject *) w);
CompDisplay *d = w->screen->display;
PrivDisplay *pd = compObjectGetPrivate((CompObject *) d);
PrivScreen *ps = compObjectGetPrivate((CompObject *) w->screen);
/* free existing data structures */
for (unsigned long i = 0; i < pw->nRegions; ++i)
{
if (pw->pRegion[i].xRegion != 0) {
XDestroyRegion(pw->pRegion[i].xRegion);
}
if(pw->pRegion[i].cc)
{
for(unsigned long j = 0; j < ps->nContexts; ++j)
{
if(pw->pRegion[i].cc[j])
{
oyProfile_Release( &pw->pRegion[i].cc[j]->dst_profile );
oyProfile_Release( &pw->pRegion[i].cc[j]->src_profile );
if(&pw->pRegion[i].cc[j]->glTexture)
glDeleteTextures( 1, &pw->pRegion[i].cc[j]->glTexture );
cicc_free( pw->pRegion[i].cc[j] );
pw->pRegion[i].cc[j] = NULL;
}
else
break;
}
cicc_free( pw->pRegion[i].cc ); pw->pRegion[i].cc = 0;
}
}
if (pw->nRegions)
cicc_free(pw->pRegion);
pw->nRegions = 0;
oyRectangle_Release( &pw->absoluteWindowRectangleOld );
/* fetch the regions */
unsigned long nBytes;
void *data = fetchProperty( d->display, w->id, pd->iccColorRegions,
XA_CARDINAL, &nBytes, False );
/* allocate the list */
unsigned long count = 1;
if(data)
count += XcolorRegionCount(data, nBytes + 1);
if(oy_debug)
fprintf( stderr, DBG_STRING"XcolorRegionCount+1=%lu\n", DBG_ARGS,
count );
pw->pRegion = (PrivColorRegion*) cicc_alloc(count * sizeof(PrivColorRegion));
if (pw->pRegion == NULL)
goto out;
/* get the complete windows region and put it at the end */
pw->pRegion[count-1].xRegion = windowRegion( w );
/* fill in the possible application region(s) */
XcolorRegion *region = data;
Region wRegion = pw->pRegion[count-1].xRegion;
for (unsigned long i = 0; i < (count - 1); ++i)
{
uint8_t n[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
pw->pRegion[i].xRegion = convertRegion( d->display, ntohl(region->region) );
memcpy( pw->pRegion[i].md5, region->md5, 16 );
/* substract a application region from the window region */
XSubtractRegion( wRegion, pw->pRegion[i].xRegion, wRegion );
if(memcmp(region->md5,n,16) != 0)
{
pw->pRegion[i].cc = (PrivColorContext**)cicc_alloc( (ps->nContexts + 1) *
sizeof(PrivColorContext*));
if(!pw->pRegion[i].cc)
{
printf( DBG_STRING "Could not allocate contexts. Stop!\n",
DBG_ARGS );
goto out;
}
for(unsigned long j = 0; j < ps->nContexts; ++j)
{
pw->pRegion[i].cc[j] = (PrivColorContext*) cicc_alloc(
sizeof(PrivColorContext) );
if(!pw->pRegion[i].cc[j])
{
printf( DBG_STRING "Could not allocate context. Stop!\n",
DBG_ARGS );
goto out;
}
if(ps && ps->nContexts > 0)
{
pw->pRegion[i].cc[j]->dst_profile = oyProfile_Copy(
ps->contexts[j].cc.dst_profile, 0 );
if(!pw->pRegion[i].cc[j]->dst_profile)
{
printf( DBG_STRING "output 0 not ready\n",
DBG_ARGS );
continue;
}
pw->pRegion[i].cc[j]->src_profile = profileFromMD5(region->md5);
fprintf( stderr, DBG_STRING"region->md5: %s\n", DBG_ARGS,
oyProfile_GetText( pw->pRegion[i].cc[j]->src_profile, oyNAME_DESCRIPTION ) );
pw->pRegion[i].cc[j]->output_name = strdup(
ps->contexts[j].cc.output_name );
} else
printf( DBG_STRING "output_name: %s\n",
DBG_ARGS, ps->contexts[j].cc.output_name);
if(pw->pRegion[i].cc[j]->src_profile)
setupColourTable( pw->pRegion[i].cc[j],
getDisplayAdvanced(w->screen, 0), w->screen );
else
printf( DBG_STRING "region %lu on %lu has no source profile!\n",
DBG_ARGS, i, j );
}
} else if(oy_debug)
fprintf( stderr, DBG_STRING"no region->md5 %lu cc=0x%lx %d,%d,%dx%d\n", DBG_ARGS,
i, (unsigned long)pw->pRegion[i].cc, pw->pRegion[i].xRegion->extents.x1,
pw->pRegion[i].xRegion->extents.y1,
pw->pRegion[i].xRegion->extents.x2-pw->pRegion[i].xRegion->extents.x1,
pw->pRegion[i].xRegion->extents.y2-pw->pRegion[i].xRegion->extents.y1
);
region = XcolorRegionNext(region);
}
pw->nRegions = count;
pw->active = 1;
pw->absoluteWindowRectangleOld = oyRectangle_NewWith( 0, 0, w->serverWidth,
w->serverHeight, 0 );
addWindowDamage(w);
out:
XFree(data);
#if defined(PLUGIN_DEBUG_)
if(count > 1)
oyCompLogMessage(d, "compicc", CompLogLevelDebug, "Added %d regions",
count);
#endif
}
/**
* Called when the window target (_ICC_COLOR_TARGET) has been changed.
*/
static void updateWindowOutput(CompWindow *w)
{
PrivWindow *pw = compObjectGetPrivate((CompObject *) w);
CompDisplay *d = w->screen->display;
PrivDisplay *pd = compObjectGetPrivate((CompObject *) d);
if (pw->output)
XFree(pw->output);
unsigned long nBytes;
pw->output = fetchProperty(d->display, w->id, pd->iccColorOutputs, XA_STRING, &nBytes, False);
if(!pw->nRegions)
addWindowDamage(w);
}
static void cdCreateTexture( PrivColorContext *ccontext )
{
glBindTexture(GL_TEXTURE_3D, ccontext->glTexture);
ccontext->scale = (GLfloat) (GRIDPOINTS - 1) / GRIDPOINTS;
ccontext->offset = (GLfloat) 1.0 / (2 * GRIDPOINTS);
glGenTextures(1, &ccontext->glTexture);
glBindTexture(GL_TEXTURE_3D, ccontext->glTexture);
fprintf( stderr, DBG_STRING"glTexture=%d\n", DBG_ARGS,
ccontext->glTexture );
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage3D( GL_TEXTURE_3D, 0, GL_RGB16, GRIDPOINTS,GRIDPOINTS,GRIDPOINTS,
0, GL_RGB, GL_UNSIGNED_SHORT, ccontext->clut);
}
/* returned data is owned by user;
* free with XFree(returned_data)
*/
static oyPointer getScreenProfile ( CompScreen * s,
int screen,
int server,
size_t * size )
{
char num[12];
Window root = RootWindow( s->display->display, 0 );
char * icc_profile_atom = (char*)cicc_alloc( 1024 );
Atom a;
oyPointer data;
unsigned long n = 0;
if(!icc_profile_atom) return 0;
snprintf( num, 12, "%d", (int)screen );
if(server)
snprintf( icc_profile_atom, 1024, XCM_DEVICE_PROFILE"%s%s",
screen ? "_" : "", screen ? num : "" );
else
snprintf( icc_profile_atom, 1024, XCM_ICC_V0_3_TARGET_PROFILE_IN_X_BASE"%s%s",
screen ? "_" : "", screen ? num : "" );
a = XInternAtom(s->display->display, icc_profile_atom, False);
oyCompLogMessage( s->display, "compicc", CompLogLevelDebug,
DBG_STRING"fetching profile from %s atom: %d",
DBG_ARGS,
icc_profile_atom, a);
data = fetchProperty( s->display->display, root, a, XA_CARDINAL,
&n, False);
oyCompLogMessage( s->display, "compicc", CompLogLevelDebug,
DBG_STRING"fetching %s, found %lu: %s",
DBG_ARGS,
icc_profile_atom,
n, (data == NULL ? "no data":"some data obtained") );
*size = (size_t)n;
cicc_free(icc_profile_atom);
return data;
}
static void changeProperty ( Display * display,
Atom target_atom,
int type,
void * data,
unsigned long size )
{
const char * atom_name = XGetAtomName( display, target_atom );
oyCompLogMessage( display, "compicc", CompLogLevelDebug,
DBG_STRING"XChangeProperty atom: %s size: %lu",
DBG_ARGS,
atom_name, size );
XChangeProperty( display, RootWindow( display, 0 ),
target_atom, type, 8, PropModeReplace,
data, size );
}
static void moveICCprofileAtoms ( CompScreen * s,
int screen,
int init )
{
{
oyOptions_s * opts = 0,
* result = 0;
const char * display_name = strdup(XDisplayString(s->display->display));
oyOptions_SetFromString( &opts, "////display_name",
display_name, OY_CREATE_NEW );
oyOptions_SetFromInt( &opts, "////screen", screen, 0, OY_CREATE_NEW );
oyOptions_SetFromInt( &opts, "////setup", init, 0, OY_CREATE_NEW );
oyCompLogMessage( s->display, "compicc", CompLogLevelDebug,
DBG_STRING "Moving profiles on %s: for screen %d setup %d",
DBG_ARGS, display_name, screen, init );
//fprintf( stderr, "Moving profiles on %s: for screen %d setup %d\n",
// display_name, screen, init );
oyOptions_Handle( "//" OY_TYPE_STD "/move_color_server_profiles",
opts,"move_color_server_profiles",
&result );
oyOptions_Release( &opts );
oyOptions_Release( &result );
return;
}
}
static int getDeviceProfile ( CompScreen * s,
PrivScreen * ps,
oyConfig_s * device,
int screen )
{
PrivColorOutput * output = &ps->contexts[screen];
oyOption_s * o = 0;
oyRectangle_s * r = 0;
const char * device_name = 0;
char num[12];
int error = 0, t_err = 0;
snprintf( num, 12, "%d", (int)screen );
o = oyConfig_Find( device, "device_rectangle" );
if( !o )
{
oyCompLogMessage( s->display, "compicc", CompLogLevelWarn,
DBG_STRING"monitor rectangle request failed", DBG_ARGS);
return 1;
}
r = (oyRectangle_s*) oyOption_GetStruct( o, oyOBJECT_RECTANGLE_S );
if( !r )
{
oyCompLogMessage( s->display, "compicc", CompLogLevelWarn,
DBG_STRING"monitor rectangle request failed", DBG_ARGS);
return 1;
}
oyOption_Release( &o );
output->xRect.x = oyRectangle_GetGeo1( r, 0 );
output->xRect.y = oyRectangle_GetGeo1( r, 1 );
output->xRect.width = oyRectangle_GetGeo1( r, 2 );
output->xRect.height = oyRectangle_GetGeo1( r, 3 );
device_name = oyConfig_FindString( device, "device_name", 0 );
if(device_name && device_name[0])
{
strcpy( output->name, device_name );
} else
{
oyCompLogMessage( s->display, "compicc", CompLogLevelWarn,
DBG_STRING "oyDevicesGet list answere included no device_name",DBG_ARGS);
strcpy( output->name, num );
}
oyProfile_Release( &output->cc.dst_profile );
size_t size = 0;
int server = 1;
/* try to get the device profile atom */
oyPointer pp = getScreenProfile( s, screen, server, &size );
/* check if the normal profile atom is a device profile */
if(!pp)
{
server = 0;
pp = getScreenProfile( s, screen, server, &size );
if(!pp)
oyCompLogMessage( s->display, "compicc", CompLogLevelDebug,
DBG_STRING "no server profile on %s, size: %d",
DBG_ARGS, output->name, (int)size);
/* filter out ordinary sRGB */
if(pp)
{
oyProfile_s * web = oyProfile_FromStd( oyASSUMED_WEB,
icc_profile_flags, 0 );
output->cc.dst_profile = oyProfile_FromMem( size, pp, 0,0 );
if(oyProfile_Equal( web, output->cc.dst_profile ))
oyProfile_Release( &output->cc.dst_profile );
oyProfile_Release( &web );
} else
oyCompLogMessage( s->display, "compicc", CompLogLevelDebug,
DBG_STRING "no normal profile on %s, size: %d",
DBG_ARGS, output->name, (int)size);
}
if(pp)
{ XFree(pp); pp = NULL; }
if(output->cc.dst_profile)
{
oyCompLogMessage( s->display, "compicc", CompLogLevelDebug,
DBG_STRING "reusing existing profile on %s, size: %d",
DBG_ARGS, output->name, (int)size);
} else
{
oyOptions_s * options = 0;
oyOptions_SetFromString( &options,
"//" OY_TYPE_STD "/config/command",
"list", OY_CREATE_NEW );
oyOptions_SetFromInt( &options,
"////icc_profile_flags",
icc_profile_flags, 0, OY_CREATE_NEW );
oyOptions_SetFromString( &options,
"//" OY_TYPE_STD "/config/icc_profile.x_color_region_target",
"yes", OY_CREATE_NEW );
t_err = oyDeviceAskProfile2( device, options, &output->cc.dst_profile );
if(t_err)
oyCompLogMessage( s->display, "compicc", CompLogLevelDebug,
DBG_STRING "oyDeviceAskProfile2() returned an issue %s: %d",
DBG_ARGS, output->name, t_err);
if(!output->cc.dst_profile || t_err == -1)
{
int old_t_err = t_err;
t_err = oyDeviceGetProfile( device, options, &output->cc.dst_profile );
oyCompLogMessage( s->display, "compicc", CompLogLevelDebug,
DBG_STRING "oyDeviceAskProfile2() has \"%s\" profile on %s: %d oyDeviceGetProfile() got -> \"%s\" %d",
DBG_ARGS, output->cc.dst_profile ? oyProfile_GetText(output->cc.dst_profile, oyNAME_DESCRIPTION):"----",
output->name, old_t_err, oyProfile_GetText(output->cc.dst_profile, oyNAME_DESCRIPTION), t_err);
}
oyOptions_Release( &options );
}
if(output->cc.dst_profile)
{
/* check that no sRGB is delivered */
if(t_err)
{
oyProfile_s * web = oyProfile_FromStd( oyASSUMED_WEB, icc_profile_flags, 0 );
if(oyProfile_Equal( web, output->cc.dst_profile ))
{
oyCompLogMessage( s->display, "compicc", CompLogLevelDebug,
DBG_STRING "Output %s ignoring sRGB fallback %d %d",
DBG_ARGS, output->name, error, t_err);
oyProfile_Release( &output->cc.dst_profile );
error = 1;
}
oyProfile_Release( &web );
}
} else
{
oyCompLogMessage( s->display, "compicc", CompLogLevelWarn,
DBG_STRING "Output %s: no ICC profile found %d",