-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathasvisual.c
More file actions
2435 lines (2228 loc) · 71.6 KB
/
asvisual.c
File metadata and controls
2435 lines (2228 loc) · 71.6 KB
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
/*
* Copyright (c) 2001,2000,1999 Sasha Vasko <sasha at aftercode.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define LOCAL_DEBUG
#undef DEBUG_SL2XIMAGE
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif
#include "afterbase.h"
#include "asvisual.h"
#include "scanline.h"
#if defined(XSHMIMAGE) && !defined(X_DISPLAY_MISSING)
# include <sys/ipc.h>
# include <sys/shm.h>
# include <X11/extensions/XShm.h>
#else
# undef XSHMIMAGE
#endif
#if defined(HAVE_GLX) && !defined(X_DISPLAY_MISSING)
# include <GL/gl.h>
# include <GL/glx.h>
#else
# undef HAVE_GLX
#endif
#ifndef X_DISPLAY_MISSING
static int get_shifts (unsigned long mask);
static int get_bits (unsigned long mask);
void _XInitImageFuncPtrs(XImage*);
int
asvisual_empty_XErrorHandler (Display * dpy, XErrorEvent * event)
{
(void)dpy; // silence unused variable warning
(void)event; // silence unused variable warning
return 0;
}
/***************************************************************************/
#if defined(LOCAL_DEBUG) && !defined(NO_DEBUG_OUTPUT)
Status
debug_AllocColor( const char *file, int line, ASVisual *asv, Colormap cmap, XColor *pxcol )
{
Status sret ;
sret = XAllocColor( asv->dpy, cmap, pxcol );
show_progress( " XAllocColor in %s:%d has %s -> cmap = %lX, pixel = %lu(%8.8lX), color = 0x%4.4X, 0x%4.4X, 0x%4.4X",
file, line, (sret==0)?"failed":"succeeded", (long)cmap, (unsigned long)(pxcol->pixel), (unsigned long)(pxcol->pixel), pxcol->red, pxcol->green, pxcol->blue );
return sret;
}
#define ASV_ALLOC_COLOR(asv,cmap,pxcol) debug_AllocColor(__FILE__, __LINE__, (asv),(cmap),(pxcol))
#else
#define ASV_ALLOC_COLOR(asv,cmap,pxcol) XAllocColor((asv)->dpy,(cmap),(pxcol))
#endif
#else
#define ASV_ALLOC_COLOR(asv,cmap,pxcol) 0
#endif /* ndef X_DISPLAY_MISSING */
/**********************************************************************/
/* returns the maximum number of true colors between a and b */
long
ARGB32_manhattan_distance (long a, long b)
{
register int d = (int)ARGB32_RED8(a) - (int)ARGB32_RED8(b);
register int t = (d < 0 )? -d : d ;
d = (int)ARGB32_GREEN8(a) - (int)ARGB32_GREEN8(b);
t += (d < 0)? -d : d ;
d = (int)ARGB32_BLUE8(a) - (int)ARGB32_BLUE8(b);
return (t+((d < 0)? -d : d)) ;
}
/***************************************************************************
* ASVisual :
* encoding/decoding/querying/setup
***************************************************************************/
int get_bits_per_pixel(Display *dpy, int depth)
{
(void)dpy; // silence unused variable warning
#if 0
#ifndef X_DISPLAY_MISSING
register ScreenFormat *fmt = dpy->pixmap_format;
register int i;
for (i = dpy->nformats + 1; --i; ++fmt)
if (fmt->depth == depth)
return(fmt->bits_per_pixel);
#endif
#endif
if (depth <= 4)
return 4;
if (depth <= 8)
return 8;
if (depth <= 16)
return 16;
return 32;
}
/* ********************* ASVisual ************************************/
ASVisual *_set_default_asvisual( ASVisual *new_v );
#ifndef X_DISPLAY_MISSING
static XColor black_xcol = {0, 0x0000, 0x0000, 0x0000, DoRed | DoGreen | DoBlue, 0};
static XColor white_xcol = {0, 0xFFFF, 0xFFFF, 0xFFFF, DoRed | DoGreen | DoBlue, 0};
static void find_useable_visual( ASVisual *asv, Display *dpy, int screen,
Window root, XVisualInfo *list, int nitems,
XSetWindowAttributes *attr )
{
int k ;
int (*oldXErrorHandler) (Display *, XErrorEvent *) =
XSetErrorHandler (asvisual_empty_XErrorHandler);
Colormap orig_cmap = attr->colormap ;
for( k = 0 ; k < nitems ; k++ )
{
Window w = None, wjunk;
unsigned int width, height, ujunk ;
int junk;
/* try and use default colormap when possible : */
if( orig_cmap == None )
{
if( list[k].visual == DefaultVisual( dpy, (screen) ) )
{
attr->colormap = DefaultColormap( dpy, screen );
LOCAL_DEBUG_OUT( "Using Default colormap %lX", attr->colormap );
}else
{
attr->colormap = XCreateColormap( dpy, root, list[k].visual, AllocNone);
LOCAL_DEBUG_OUT( "DefaultVisual is %p, while ours is %p, so Created new colormap %lX", DefaultVisual( dpy, (screen) ), list[k].visual, attr->colormap );
}
}
ASV_ALLOC_COLOR( asv, attr->colormap, &black_xcol );
ASV_ALLOC_COLOR( asv, attr->colormap, &white_xcol );
attr->border_pixel = black_xcol.pixel ;
/*fprintf( stderr, "checking out visual ID %d, class %d, depth = %d mask = %X,%X,%X\n", list[k].visualid, list[k].class, list[k].depth, list[k].red_mask, list[k].green_mask, list[k].blue_mask );*/
w = XCreateWindow (dpy, root, -10, -10, 10, 10, 0, list[k].depth, CopyFromParent, list[k].visual, CWColormap|CWBorderPixel, attr );
if( w != None && XGetGeometry (dpy, w, &wjunk, &junk, &junk, &width, &height, &ujunk, &ujunk))
{
/* don't really care what's in it since we do not use it anyways : */
asv->visual_info = list[k] ;
XDestroyWindow( dpy, w );
asv->colormap = attr->colormap ;
asv->own_colormap = (attr->colormap != DefaultColormap( dpy, screen ));
asv->white_pixel = white_xcol.pixel ;
asv->black_pixel = black_xcol.pixel ;
break;
}
if( orig_cmap == None )
{
if( attr->colormap != DefaultColormap( dpy, screen ))
XFreeColormap( dpy, attr->colormap );
attr->colormap = None ;
}
}
XSetErrorHandler(oldXErrorHandler);
}
#endif
/* Main procedure finding and querying the best visual */
Bool
query_screen_visual_id( ASVisual *asv, Display *dpy, int screen, Window root, int default_depth, VisualID visual_id, Colormap cmap )
{
#ifndef X_DISPLAY_MISSING
int nitems = 0 ;
/* first - attempt locating 24bpp TrueColor or DirectColor RGB or BGR visuals as the best cases : */
/* second - attempt locating 32bpp TrueColor or DirectColor RGB or BGR visuals as the next best cases : */
/* third - lesser but still capable 16bpp 565 RGB or BGR modes : */
/* forth - even more lesser 15bpp 555 RGB or BGR modes : */
/* nothing nice has been found - use whatever X has to offer us as a default :( */
int i ;
XVisualInfo *list = NULL;
XSetWindowAttributes attr ;
static XVisualInfo templates[] =
/* Visual, visualid, screen, depth, class , red_mask, green_mask, blue_mask, colormap_size, bits_per_rgb */
{{ NULL , 0 , 0 , 24 , TrueColor , 0xFF0000, 0x00FF00 , 0x0000FF , 0 , 0 },
{ NULL , 0 , 0 , 24 , TrueColor , 0x0000FF, 0x00FF00 , 0xFF0000 , 0 , 0 },
{ NULL , 0 , 0 , 24 , TrueColor , 0x0 , 0x0 , 0x0 , 0 , 0 },
{ NULL , 0 , 0 , 32 , TrueColor , 0xFF0000, 0x00FF00 , 0x0000FF , 0 , 0 },
{ NULL , 0 , 0 , 32 , TrueColor , 0x0000FF, 0x00FF00 , 0xFF0000 , 0 , 0 },
{ NULL , 0 , 0 , 32 , TrueColor , 0x0 , 0x0 , 0x0 , 0 , 0 },
{ NULL , 0 , 0 , 16 , TrueColor , 0xF800 , 0x07E0 , 0x001F , 0 , 0 },
{ NULL , 0 , 0 , 16 , TrueColor , 0x001F , 0x07E0 , 0xF800 , 0 , 0 },
/* big endian or MBR_First modes : */
{ NULL , 0 , 0 , 16 , TrueColor , 0x0 , 0xE007 , 0x0 , 0 , 0 },
/* some misrepresented modes that really are 15bpp : */
{ NULL , 0 , 0 , 16 , TrueColor , 0x7C00 , 0x03E0 , 0x001F , 0 , 0 },
{ NULL , 0 , 0 , 16 , TrueColor , 0x001F , 0x03E0 , 0x7C00 , 0 , 0 },
{ NULL , 0 , 0 , 16 , TrueColor , 0x0 , 0xE003 , 0x0 , 0 , 0 },
{ NULL , 0 , 0 , 15 , TrueColor , 0x7C00 , 0x03E0 , 0x001F , 0 , 0 },
{ NULL , 0 , 0 , 15 , TrueColor , 0x001F , 0x03E0 , 0x7C00 , 0 , 0 },
{ NULL , 0 , 0 , 15 , TrueColor , 0x0 , 0xE003 , 0x0 , 0 , 0 },
/* no suitable TrueColorMode found - now do the same thing to DirectColor :*/
{ NULL , 0 , 0 , 24 , DirectColor, 0xFF0000, 0x00FF00 , 0x0000FF , 0 , 0 },
{ NULL , 0 , 0 , 24 , DirectColor, 0x0000FF, 0x00FF00 , 0xFF0000 , 0 , 0 },
{ NULL , 0 , 0 , 24 , DirectColor, 0x0 , 0x0 , 0x0 , 0 , 0 },
{ NULL , 0 , 0 , 32 , DirectColor, 0xFF0000, 0x00FF00 , 0x0000FF , 0 , 0 },
{ NULL , 0 , 0 , 32 , DirectColor, 0x0000FF, 0x00FF00 , 0xFF0000 , 0 , 0 },
{ NULL , 0 , 0 , 32 , DirectColor, 0x0 , 0x0 , 0x0 , 0 , 0 },
{ NULL , 0 , 0 , 16 , DirectColor, 0xF800 , 0x07E0 , 0x001F , 0 , 0 },
{ NULL , 0 , 0 , 16 , DirectColor, 0x001F , 0x07E0 , 0xF800 , 0 , 0 },
{ NULL , 0 , 0 , 16 , DirectColor, 0x0 , 0xE007 , 0x0 , 0 , 0 },
{ NULL , 0 , 0 , 16 , DirectColor, 0x7C00 , 0x03E0 , 0x001F , 0 , 0 },
{ NULL , 0 , 0 , 16 , DirectColor, 0x001F , 0x03E0 , 0x7C00 , 0 , 0 },
{ NULL , 0 , 0 , 16 , DirectColor, 0x0 , 0xE003 , 0x0 , 0 , 0 },
{ NULL , 0 , 0 , 15 , DirectColor, 0x7C00 , 0x03E0 , 0x001F , 0 , 0 },
{ NULL , 0 , 0 , 15 , DirectColor, 0x001F , 0x03E0 , 0x7C00 , 0 , 0 },
{ NULL , 0 , 0 , 15 , DirectColor, 0x0 , 0xE003 , 0x0 , 0 , 0 },
{ NULL , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },
} ;
#else
(void)screen;
(void)root;
(void)default_depth;
(void)visual_id;
(void)cmap; // silence unused variable warning
#endif /*ifndef X_DISPLAY_MISSING */
if( asv == NULL )
return False ;
memset( asv, 0x00, sizeof(ASVisual));
asv->dpy = dpy ;
#ifndef X_DISPLAY_MISSING
memset( &attr, 0x00, sizeof( attr ));
attr.colormap = cmap ;
if( visual_id == 0 )
{
for( i = 0 ; templates[i].depth != 0 ; i++ )
{
int mask = VisualScreenMask|VisualDepthMask|VisualClassMask ;
templates[i].screen = screen ;
if( templates[i].red_mask != 0 )
mask |= VisualRedMaskMask;
if( templates[i].green_mask != 0 )
mask |= VisualGreenMaskMask ;
if( templates[i].blue_mask != 0 )
mask |= VisualBlueMaskMask ;
if( (list = XGetVisualInfo( dpy, mask, &(templates[i]), &nitems ))!= NULL )
{
find_useable_visual( asv, dpy, screen, root, list, nitems, &attr );
XFree( list );
list = NULL ;
if( asv->visual_info.visual != NULL )
break;
}
}
}else
{
templates[0].visualid = visual_id ;
if( (list = XGetVisualInfo( dpy, VisualIDMask, &(templates[0]), &nitems )) != NULL )
{
find_useable_visual( asv, dpy, screen, root, list, nitems, &attr );
XFree( list );
list = NULL ;
}
if( asv->visual_info.visual == NULL )
show_error( "Visual with requested ID of 0x%X is unusable - will try default instead.", visual_id );
}
if( asv->visual_info.visual == NULL )
{ /* we ain't found any decent Visuals - that's some crappy screen you have! */
register int vclass = 6 ;
while( --vclass >= 0 )
if( XMatchVisualInfo( dpy, screen, default_depth, vclass, &(asv->visual_info) ) )
break;
if( vclass < 0 )
return False;
/* try and use default colormap when possible : */
if( asv->visual_info.visual == DefaultVisual( dpy, screen ) )
attr.colormap = DefaultColormap( dpy, screen );
else
attr.colormap = XCreateColormap( dpy, root, asv->visual_info.visual, AllocNone);
ASV_ALLOC_COLOR( asv, attr.colormap, &black_xcol );
ASV_ALLOC_COLOR( asv, attr.colormap, &white_xcol );
asv->colormap = attr.colormap ;
asv->own_colormap = (attr.colormap != DefaultColormap( dpy, screen ));
asv->white_pixel = white_xcol.pixel ;
asv->black_pixel = black_xcol.pixel ;
}
if( get_output_threshold() >= OUTPUT_VERBOSE_THRESHOLD )
{
fprintf( stderr, "Selected visual 0x%lx: depth %d, class %d\n RGB masks: 0x%lX, 0x%lX, 0x%lX, Byte Ordering: %s\n",
(unsigned long)asv->visual_info.visualid,
asv->visual_info.depth,
asv->visual_info.class,
(unsigned long)asv->visual_info.red_mask,
(unsigned long)asv->visual_info.green_mask,
(unsigned long)asv->visual_info.blue_mask,
(ImageByteOrder(asv->dpy)==MSBFirst)?"MSBFirst":"LSBFirst" );
}
#else
asv->white_pixel = ARGB32_White ;
asv->black_pixel = ARGB32_Black ;
#endif /*ifndef X_DISPLAY_MISSING */
return True;
}
ASVisual *
create_asvisual_for_id( Display *dpy, int screen, int default_depth, VisualID visual_id, Colormap cmap, ASVisual *reusable_memory )
{
ASVisual *asv = reusable_memory ;
#ifndef X_DISPLAY_MISSING
Window root = dpy?RootWindow(dpy,screen):None;
#endif /*ifndef X_DISPLAY_MISSING */
if( asv == NULL )
asv = safecalloc( 1, sizeof(ASVisual) );
#ifndef X_DISPLAY_MISSING
if( dpy )
{
if( query_screen_visual_id( asv, dpy, screen, root, default_depth, visual_id, cmap ) )
{ /* found visual - now off to decide about color handling on it : */
if( !setup_truecolor_visual( asv ) )
{ /* well, we don't - lets try and preallocate as many colors as we can but up to
* 1/4 of the colorspace or 12bpp colors, whichever is smaller */
setup_pseudo_visual( asv );
if( asv->as_colormap == NULL )
setup_as_colormap( asv );
}
}else
{
if( reusable_memory != asv )
free( asv );
asv = NULL ;
}
}
#else
(void)dpy;
(void)screen;
(void)default_depth;
(void)visual_id;
(void)cmap;
(void)reusable_memory; // silence unused variable warning
#endif /*ifndef X_DISPLAY_MISSING */
_set_default_asvisual( asv );
return asv;
}
ASVisual *
create_asvisual( Display *dpy, int screen, int default_depth, ASVisual *reusable_memory )
{
VisualID visual_id = 0;
char *id_env_var ;
if( (id_env_var = getenv( ASVISUAL_ID_ENVVAR )) != NULL )
visual_id = strtol(id_env_var,NULL,16);
return create_asvisual_for_id( dpy, screen, default_depth, visual_id, None, reusable_memory );
}
void
destroy_asvisual( ASVisual *asv, Bool reusable )
{
if( asv )
{
if( get_default_asvisual() == asv )
_set_default_asvisual( NULL );
#ifndef X_DISPLAY_MISSING
if( asv->own_colormap )
{
if( asv->colormap )
XFreeColormap( asv->dpy, asv->colormap );
}
if( asv->as_colormap )
{
free( asv->as_colormap );
if( asv->as_colormap_reverse.xref != NULL )
{
if( asv->as_colormap_type == ACM_12BPP )
destroy_ashash( &(asv->as_colormap_reverse.hash) );
else
free( asv->as_colormap_reverse.xref );
}
}
#ifdef HAVE_GLX
if( asv->glx_scratch_gc_direct )
glXDestroyContext(asv->dpy, asv->glx_scratch_gc_direct );
if( asv->glx_scratch_gc_indirect )
glXDestroyContext(asv->dpy, asv->glx_scratch_gc_indirect );
#endif
if( asv->scratch_window )
XDestroyWindow( asv->dpy, asv->scratch_window );
#endif /*ifndef X_DISPLAY_MISSING */
if( !reusable )
free( asv );
}
}
int as_colormap_type2size( int type )
{
switch( type )
{
case ACM_3BPP :
return 8 ;
case ACM_6BPP :
return 64 ;
case ACM_12BPP :
return 4096 ;
default:
return 0 ;
}
}
Bool
visual2visual_prop( ASVisual *asv, size_t *size_ret,
unsigned long *version_ret,
unsigned long **data_ret )
{
int cmap_size = 0 ;
unsigned long *prop ;
size_t size;
if( asv == NULL || data_ret == NULL)
return False;
cmap_size = as_colormap_type2size( asv->as_colormap_type );
if( cmap_size > 0 && asv->as_colormap == NULL )
return False ;
size = (1+1+2+1+cmap_size)*sizeof(unsigned long);
prop = safemalloc( size ) ;
#ifndef X_DISPLAY_MISSING
prop[0] = asv->visual_info.visualid ;
prop[1] = asv->colormap ;
prop[2] = asv->black_pixel ;
prop[3] = asv->white_pixel ;
prop[4] = asv->as_colormap_type ;
if( cmap_size > 0 )
{
register int i;
for( i = 0 ; i < cmap_size ; i++ )
prop[i+5] = asv->as_colormap[i] ;
}
if( size_ret )
*size_ret = size;
#else
(void)size_ret; // silence unused variable warning
#endif /*ifndef X_DISPLAY_MISSING */
if (version_ret)
*version_ret = (1 << 16) + 0; /* version is 1.0 */
*data_ret = prop;
return True;
}
Bool
visual_prop2visual( ASVisual *asv, Display *dpy, int screen,
size_t size,
unsigned long version,
unsigned long *data )
{
#ifndef X_DISPLAY_MISSING
XVisualInfo templ, *list ;
int nitems = 0 ;
int cmap_size = 0 ;
#endif /*ifndef X_DISPLAY_MISSING */
if( asv == NULL )
return False;
asv->dpy = dpy ;
if( size < (1+1+2+1)*sizeof(unsigned long) ||
(version&0x00FFFF) != 0 || (version>>16) != 1 || data == NULL )
return False;
if( data[0] == None || data[1] == None ) /* we MUST have valid colormap and visualID !!!*/
return False;
#ifndef X_DISPLAY_MISSING
templ.screen = screen ;
templ.visualid = data[0] ;
list = XGetVisualInfo( dpy, VisualScreenMask|VisualIDMask, &templ, &nitems );
if( list == NULL || nitems == 0 )
return False; /* some very bad visual ID has been requested :( */
asv->visual_info = *list ;
XFree( list );
if( asv->own_colormap && asv->colormap )
XFreeColormap( dpy, asv->colormap );
asv->colormap = data[1] ;
asv->own_colormap = False ;
asv->black_pixel = data[2] ;
asv->white_pixel = data[3] ;
asv->as_colormap_type = data[4];
cmap_size = as_colormap_type2size( asv->as_colormap_type );
if( cmap_size > 0 )
{
register int i ;
if( asv->as_colormap )
free( asv->as_colormap );
asv->as_colormap = safemalloc( cmap_size );
for( i = 0 ; i < cmap_size ; i++ )
asv->as_colormap[i] = data[i+5];
}else
asv->as_colormap_type = ACM_None ; /* just in case */
#else
(void)screen; // silence unused variable warning
#endif /*ifndef X_DISPLAY_MISSING */
return True;
}
Bool
setup_truecolor_visual( ASVisual *asv )
{
#ifndef X_DISPLAY_MISSING
XVisualInfo *vi = &(asv->visual_info) ;
if( vi->class != TrueColor )
return False;
#ifdef HAVE_GLX
if( glXQueryExtension (asv->dpy, NULL, NULL))
{
int val = False;
glXGetConfig(asv->dpy, vi, GLX_USE_GL, &val);
if( val )
{
asv->glx_scratch_gc_indirect = glXCreateContext (asv->dpy, &(asv->visual_info), NULL, False);
if( asv->glx_scratch_gc_indirect )
{
set_flags( asv->glx_support, ASGLX_Available );
if( glXGetConfig(asv->dpy, vi, GLX_RGBA, &val) == 0 )
if( val ) set_flags( asv->glx_support, ASGLX_RGBA );
if( glXGetConfig(asv->dpy, vi, GLX_DOUBLEBUFFER, &val) == 0 )
if( val ) set_flags( asv->glx_support, ASGLX_DoubleBuffer );
if( glXGetConfig(asv->dpy, vi, GLX_DOUBLEBUFFER, &val) == 0 )
if( val ) set_flags( asv->glx_support, ASGLX_DoubleBuffer );
if( (asv->glx_scratch_gc_direct = glXCreateContext (asv->dpy, &(asv->visual_info), NULL, True)) != NULL )
if( !glXIsDirect( asv->dpy, asv->glx_scratch_gc_direct ) )
{
glXDestroyContext(asv->dpy, asv->glx_scratch_gc_direct );
asv->glx_scratch_gc_direct = NULL ;
}
#if 0 /* that needs some more research : */
/* under Cygwin that seems to be 40% faster then regular XImage for some reason */
set_flags( asv->glx_support, ASGLX_UseForImageTx );
#endif
}
}
}
#endif
asv->BGR_mode = ((vi->red_mask&0x0010)!=0) ;
asv->rshift = get_shifts (vi->red_mask);
asv->gshift = get_shifts (vi->green_mask);
asv->bshift = get_shifts (vi->blue_mask);
asv->rbits = get_bits (vi->red_mask);
asv->gbits = get_bits (vi->green_mask);
asv->bbits = get_bits (vi->blue_mask);
asv->true_depth = vi->depth ;
asv->msb_first = (ImageByteOrder(asv->dpy)==MSBFirst);
if( asv->true_depth == 16 && ((vi->red_mask|vi->blue_mask)&0x8000) == 0 )
asv->true_depth = 15;
/* setting up conversion handlers : */
switch( asv->true_depth )
{
case 24 :
case 32 :
asv->color2pixel_func = (asv->BGR_mode)?color2pixel32bgr:color2pixel32rgb ;
asv->pixel2color_func = (asv->BGR_mode)?pixel2color32bgr:pixel2color32rgb ;
asv->ximage2scanline_func = ximage2scanline32 ;
asv->scanline2ximage_func = scanline2ximage32 ;
break ;
/* case 24 :
scr->color2pixel_func = (bgr_mode)?color2pixel24bgr:color2pixel24rgb ;
scr->pixel2color_func = (bgr_mode)?pixel2color24bgr:pixel2color24rgb ;
scr->ximage2scanline_func = ximage2scanline24 ;
scr->scanline2ximage_func = scanline2ximage24 ;
break ;
*/ case 16 :
asv->color2pixel_func = (asv->BGR_mode)?color2pixel16bgr:color2pixel16rgb ;
asv->pixel2color_func = (asv->BGR_mode)?pixel2color16bgr:pixel2color16rgb ;
asv->ximage2scanline_func = ximage2scanline16 ;
asv->scanline2ximage_func = scanline2ximage16 ;
break ;
case 15 :
asv->color2pixel_func = (asv->BGR_mode)?color2pixel15bgr:color2pixel15rgb ;
asv->pixel2color_func = (asv->BGR_mode)?pixel2color15bgr:pixel2color15rgb ;
asv->ximage2scanline_func = ximage2scanline15 ;
asv->scanline2ximage_func = scanline2ximage15 ;
break ;
}
#endif /*ifndef X_DISPLAY_MISSING */
return (asv->ximage2scanline_func != NULL) ;
}
ARGB32 *
make_reverse_colormap( unsigned long *cmap, size_t size, int depth, unsigned short mask, unsigned short shift )
{
unsigned int max_pixel = 0x01<<depth ;
ARGB32 *rcmap = safecalloc( max_pixel, sizeof( ARGB32 ) );
register int i ;
for( i = 0 ; i < (int)size ; i++ )
if( cmap[i] < max_pixel )
rcmap[cmap[i]] = MAKE_ARGB32( 0xFF, (i>>(shift<<1))& mask, (i>>(shift))&mask, i&mask);
return rcmap;
}
ASHashTable *
make_reverse_colorhash( unsigned long *cmap, size_t size, int depth, unsigned short mask, unsigned short shift )
{
(void)depth; // silence unused variable warning
ASHashTable *hash = create_ashash( 0, NULL, NULL, NULL );
register unsigned int i ;
if( hash )
{
for( i = 0 ; i < size ; i++ )
add_hash_item( hash, (ASHashableValue)cmap[i], (void*)((intptr_t)MAKE_ARGB32( 0xFF, (i>>(shift<<1))& mask, (i>>(shift))&mask, i&mask)) );
}
return hash;
}
void
setup_pseudo_visual( ASVisual *asv )
{
#ifndef X_DISPLAY_MISSING
XVisualInfo *vi = &(asv->visual_info) ;
/* we need to allocate new usable list of colors based on available bpp */
asv->true_depth = vi->depth ;
if( asv->as_colormap == NULL )
{
if( asv->true_depth < 8 )
asv->as_colormap_type = ACM_3BPP ;
else if( asv->true_depth < 12 )
asv->as_colormap_type = ACM_6BPP ;
else
asv->as_colormap_type = ACM_12BPP ;
}
/* then we need to set up hooks : */
switch( asv->as_colormap_type )
{
case ACM_3BPP:
asv->ximage2scanline_func = ximage2scanline_pseudo3bpp ;
asv->scanline2ximage_func = scanline2ximage_pseudo3bpp ;
asv->color2pixel_func = color2pixel_pseudo3bpp ;
break ;
case ACM_6BPP:
asv->ximage2scanline_func = ximage2scanline_pseudo6bpp ;
asv->scanline2ximage_func = scanline2ximage_pseudo6bpp ;
asv->color2pixel_func = color2pixel_pseudo6bpp ;
break ;
default:
asv->as_colormap_type = ACM_12BPP ;
case ACM_12BPP:
asv->ximage2scanline_func = ximage2scanline_pseudo12bpp ;
asv->scanline2ximage_func = scanline2ximage_pseudo12bpp ;
asv->color2pixel_func = color2pixel_pseudo12bpp ;
break ;
}
if( asv->as_colormap != NULL )
{
if( asv->as_colormap_type == ACM_3BPP || asv->as_colormap_type == ACM_6BPP )
{
unsigned short mask = 0x0003, shift = 2 ;
if( asv->as_colormap_type==ACM_3BPP )
{
mask = 0x0001 ;
shift = 1 ;
}
asv->as_colormap_reverse.xref = make_reverse_colormap( asv->as_colormap,
as_colormap_type2size( asv->as_colormap_type ),
asv->true_depth, mask, shift );
}else if( asv->as_colormap_type == ACM_12BPP )
{
asv->as_colormap_reverse.hash = make_reverse_colorhash( asv->as_colormap,
as_colormap_type2size( asv->as_colormap_type ),
asv->true_depth, 0x000F, 4 );
}
}
#else
(void)asv; // silence unused variable warning
#endif /*ifndef X_DISPLAY_MISSING */
}
#ifndef X_DISPLAY_MISSING
static unsigned long*
make_3bpp_colormap( ASVisual *asv )
{
XColor colors_3bpp[8] =
/* list of non-white, non-black colors in order of decreasing importance: */
{ { 0, 0, 0xFFFF, 0, DoRed|DoGreen|DoBlue, 0},
{ 0, 0xFFFF, 0, 0, DoRed|DoGreen|DoBlue, 0},
{ 0, 0, 0, 0xFFFF, DoRed|DoGreen|DoBlue, 0},
{ 0, 0xFFFF, 0xFFFF, 0, DoRed|DoGreen|DoBlue, 0},
{ 0, 0, 0xFFFF, 0xFFFF, DoRed|DoGreen|DoBlue, 0},
{ 0, 0xFFFF, 0, 0xFFFF, DoRed|DoGreen|DoBlue, 0}} ;
unsigned long *cmap ;
cmap = safemalloc( 8 * sizeof(unsigned long) );
/* fail safe code - if any of the alloc fails - colormap entry will still have
* most suitable valid value ( black or white in 1bpp mode for example ) : */
cmap[0] = cmap[1] = cmap[2] = cmap[3] = asv->black_pixel ;
cmap[7] = cmap[6] = cmap[5] = cmap[4] = asv->white_pixel ;
if( ASV_ALLOC_COLOR( asv, asv->colormap, &colors_3bpp[0] )) /* pure green */
cmap[0x02] = cmap[0x03] = cmap[0x06] = colors_3bpp[0].pixel ;
if( ASV_ALLOC_COLOR( asv, asv->colormap, &colors_3bpp[1] )) /* pure red */
cmap[0x04] = cmap[0x05] = colors_3bpp[1].pixel ;
if( ASV_ALLOC_COLOR( asv, asv->colormap, &colors_3bpp[2] )) /* pure blue */
cmap[0x01] = colors_3bpp[2].pixel ;
if( ASV_ALLOC_COLOR( asv, asv->colormap, &colors_3bpp[3] )) /* yellow */
cmap[0x06] = colors_3bpp[3].pixel ;
if( ASV_ALLOC_COLOR( asv, asv->colormap, &colors_3bpp[4] )) /* cyan */
cmap[0x03] = colors_3bpp[4].pixel ;
if( ASV_ALLOC_COLOR( asv, asv->colormap, &colors_3bpp[5] )) /* magenta */
cmap[0x05] = colors_3bpp[5].pixel ;
return cmap;
}
static unsigned long*
make_6bpp_colormap( ASVisual *asv, unsigned long *cmap_3bpp )
{
unsigned short red, green, blue ;
unsigned long *cmap = safemalloc( 0x0040*sizeof( unsigned long) );
XColor xcol ;
cmap[0] = asv->black_pixel ;
xcol.flags = DoRed|DoGreen|DoBlue ;
for( blue = 0 ; blue <= 0x0003 ; blue++ )
{
xcol.blue = (0xFFFF*blue)/3 ;
for( red = 0 ; red <= 0x0003 ; red++ )
{ /* red has highier priority then blue */
xcol.red = (0xFFFF*red)/3 ;
/* green = ( blue == 0 && red == 0 )?1:0 ; */
for( green = 0 ; green <= 0x0003 ; green++ )
{ /* green has highier priority then red */
unsigned short index_3bpp = ((red&0x0002)<<1)|(green&0x0002)|((blue&0x0002)>>1);
unsigned short index_6bpp = (red<<4)|(green<<2)|blue;
xcol.green = (0xFFFF*green)/3 ;
if( (red&0x0001) == ((red&0x0002)>>1) &&
(green&0x0001) == ((green&0x0002)>>1) &&
(blue&0x0001) == ((blue&0x0002)>>1) )
cmap[index_6bpp] = cmap_3bpp[index_3bpp];
else
{
if( ASV_ALLOC_COLOR( asv, asv->colormap, &xcol) != 0 )
cmap[index_6bpp] = xcol.pixel ;
else
cmap[index_6bpp] = cmap_3bpp[index_3bpp] ;
}
}
}
}
return cmap;
}
static unsigned long*
make_9bpp_colormap( ASVisual *asv, unsigned long *cmap_6bpp )
{
unsigned long *cmap = safemalloc( 512*sizeof( unsigned long) );
unsigned short red, green, blue ;
XColor xcol ;
cmap[0] = asv->black_pixel ; /* just in case */
xcol.flags = DoRed|DoGreen|DoBlue ;
for( blue = 0 ; blue <= 0x0007 ; blue++ )
{
xcol.blue = (0xFFFF*blue)/7 ;
for( red = 0 ; red <= 0x0007 ; red++ )
{ /* red has highier priority then blue */
xcol.red = (0xFFFF*red)/7 ;
for( green = 0 ; green <= 0x0007 ; green++ )
{ /* green has highier priority then red */
unsigned short index_6bpp = ((red&0x0006)<<3)|((green&0x0006)<<1)|((blue&0x0006)>>1);
unsigned short index_9bpp = (red<<6)|(green<<3)|blue;
xcol.green = (0xFFFF*green)/7 ;
if( (red&0x0001) == ((red&0x0002)>>1) &&
(green&0x0001) == ((green&0x0002)>>1) &&
(blue&0x0001) == ((blue&0x0002)>>1) )
cmap[index_9bpp] = cmap_6bpp[index_6bpp];
else
{
if( ASV_ALLOC_COLOR( asv, asv->colormap, &xcol) != 0 )
cmap[index_9bpp] = xcol.pixel ;
else
cmap[index_9bpp] = cmap_6bpp[index_6bpp] ;
}
}
}
}
return cmap;
}
static unsigned long*
make_12bpp_colormap( ASVisual *asv, unsigned long *cmap_9bpp )
{
unsigned long *cmap = safemalloc( 4096*sizeof( unsigned long) );
unsigned short red, green, blue ;
XColor xcol ;
cmap[0] = asv->black_pixel ; /* just in case */
xcol.flags = DoRed|DoGreen|DoBlue ;
for( blue = 0 ; blue <= 0x000F ; blue++ )
{
xcol.blue = (0xFFFF*blue)/15 ;
for( red = 0 ; red <= 0x000F ; red++ )
{ /* red has highier priority then blue */
xcol.red = (0xFFFF*red)/15 ;
for( green = 0 ; green <= 0x000F ; green++ )
{ /* green has highier priority then red */
unsigned short index_9bpp = ((red&0x000E)<<5)|((green&0x000E)<<2)|((blue&0x000E)>>1);
unsigned short index_12bpp = (red<<8)|(green<<4)|blue;
xcol.green = (0xFFFF*green)/15 ;
if( (red&0x0001) == ((red&0x0002)>>1) &&
(green&0x0001) == ((green&0x0002)>>1) &&
(blue&0x0001) == ((blue&0x0002)>>1) )
cmap[index_12bpp] = cmap_9bpp[index_9bpp];
else
{
if( ASV_ALLOC_COLOR( asv, asv->colormap, &xcol) != 0 )
cmap[index_12bpp] = xcol.pixel ;
else
cmap[index_12bpp] = cmap_9bpp[index_9bpp] ;
}
}
}
}
return cmap;
}
#endif /*ifndef X_DISPLAY_MISSING */
void
setup_as_colormap( ASVisual *asv )
{
#ifndef X_DISPLAY_MISSING
unsigned long *cmap_lower, *cmap ;
if( asv == NULL || asv->as_colormap != NULL )
return ;
cmap = make_3bpp_colormap( asv );
if( asv->as_colormap_type == ACM_3BPP )
{
asv->as_colormap = cmap ;
asv->as_colormap_reverse.xref = make_reverse_colormap( cmap, 8, asv->true_depth, 0x0001, 1 );
return ;
}
cmap_lower = cmap ;
cmap = make_6bpp_colormap( asv, cmap_lower );
free( cmap_lower );
if( asv->as_colormap_type == ACM_6BPP )
{
asv->as_colormap = cmap ;
asv->as_colormap_reverse.xref = make_reverse_colormap( cmap, 64, asv->true_depth, 0x0003, 2 );
}else
{
cmap_lower = cmap ;
cmap = make_9bpp_colormap( asv, cmap_lower );
free( cmap_lower );
cmap_lower = cmap ;
cmap = make_12bpp_colormap( asv, cmap_lower );
free( cmap_lower );
asv->as_colormap = cmap ;
asv->as_colormap_reverse.hash = make_reverse_colorhash( cmap, 4096, asv->true_depth, 0x000F, 4 );
}
#else
(void)asv; // silence unused variable warning
#endif /*ifndef X_DISPLAY_MISSING */
}
/*********************************************************************/
/* handy utility functions for creation of windows/pixmaps/XImages : */
/*********************************************************************/
Window
create_visual_window( ASVisual *asv, Window parent,
int x, int y, unsigned int width, unsigned int height,
unsigned int border_width, unsigned int wclass,
unsigned long mask, XSetWindowAttributes *attributes )
{
#ifndef X_DISPLAY_MISSING
XSetWindowAttributes my_attr ;
int depth = 0;
if( asv == NULL || parent == None )
return None ;
LOCAL_DEBUG_OUT( "Colormap %lX, parent %lX, %ux%u%+d%+d, bw = %d, class %d",
asv->colormap, parent, width, height, x, y, border_width,
wclass );
if( attributes == NULL )
{
attributes = &my_attr ;
memset( attributes, 0x00, sizeof(XSetWindowAttributes));
mask = 0;
}
if( width < 1 )
width = 1 ;
if( height < 1 )
height = 1 ;
if( wclass == InputOnly )
{
border_width = 0 ;
if( (mask&INPUTONLY_LEGAL_MASK) != mask )
show_warning( " software BUG detected : illegal InputOnly window's mask 0x%lX - overriding", mask );
mask &= INPUTONLY_LEGAL_MASK ;
}else
{
depth = asv->visual_info.depth ;
if( !get_flags(mask, CWColormap ) )
{
attributes->colormap = asv->colormap ;
set_flags(mask, CWColormap );
}
if( !get_flags(mask, CWBorderPixmap ) )
{
attributes->border_pixmap = None ;
set_flags(mask, CWBorderPixmap );
}
clear_flags(mask, CWBorderPixmap );
if( !get_flags(mask, CWBorderPixel ) )
{
attributes->border_pixel = asv->black_pixel ;
set_flags(mask, CWBorderPixel );
}
/* If the parent window and the new window have different bit
** depths (such as on a Solaris box with 8bpp root window and
** 24bpp child windows), ParentRelative will not work. */
if ( get_flags(mask, CWBackPixmap) && attributes->background_pixmap == ParentRelative &&
asv->visual_info.visual != DefaultVisual( asv->dpy, DefaultScreen(asv->dpy) ))
{
clear_flags(mask, CWBackPixmap);
}
}
LOCAL_DEBUG_OUT( "parent = %lX, mask = 0x%lX, VisualID = 0x%lX, Border Pixel = %ld, colormap = %lX",
parent, mask, asv->visual_info.visual->visualid, attributes->border_pixel, attributes->colormap );
return XCreateWindow (asv->dpy, parent, x, y, width, height, border_width, depth,
wclass, asv->visual_info.visual,
mask, attributes);
#else
(void)asv;
(void)parent;
(void)x;
(void)y;
(void)width;
(void)height;
(void)border_width;
(void)wclass;
(void)mask;
(void)attributes; // silence unused variable warning
return None;
#endif /*ifndef X_DISPLAY_MISSING */
}