1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include "Imlib.h"
6 #include "Imlib_private.h"
7 #include <locale.h>
8 #include <stdio.h>
9 #include "libcommon.h"
10
11 #ifdef HAVE_SHM
12 #include <X11/extensions/XShm.h>
13 #endif
14
15 #include <xine/xineutils.h>
16
17 #ifdef __EMX__
18 extern const char *__XOS2RedirRoot(const char *);
19 #endif
20
21 char x_error;
22
23 #ifdef HAVE_SHM
24 static void
HandleXError(Display * d,XErrorEvent * ev)25 HandleXError(Display * d, XErrorEvent * ev)
26 {
27 x_error = 1;
28 }
29 #endif
30
31 int
Imlib_get_render_type(ImlibData * id)32 Imlib_get_render_type(ImlibData * id)
33 {
34 if (id)
35 return id->render_type;
36 else
37 {
38 fprintf(stderr, "IMLIB ERROR: Imlib not initialised\n");
39 return -1;
40 }
41 }
42
43 void
Imlib_set_render_type(ImlibData * id,int rend_type)44 Imlib_set_render_type(ImlibData * id, int rend_type)
45 {
46 if (id)
47 {
48 if (id->x.depth > 8)
49 id->render_type = rend_type;
50 else
51 {
52 if ((rend_type == RT_PLAIN_TRUECOL) ||
53 (rend_type == RT_DITHER_TRUECOL))
54 id->render_type = RT_DITHER_PALETTE_FAST;
55 else
56 id->render_type = rend_type;
57 }
58 return;
59 }
60 else
61 {
62 fprintf(stderr, "IMLIB ERROR: Imlib not initialised\n");
63 return;
64 }
65 }
66
67 #ifdef HAVE_SHM
68 int
69 XShmGetEventBase(Display * disp);
70
71 #endif
72
73 ImlibData *
Imlib_init(Display * disp)74 Imlib_init(Display * disp)
75 {
76 ImlibData *id;
77 XWindowAttributes xwa;
78 XVisualInfo xvi, *xvir;
79 char *s, *s1;
80 FILE *f;
81 int override = 0;
82 int dither = 0;
83 int remap = 1;
84 int num;
85 int i, max, maxn;
86 int clas;
87 char *palfile;
88 int loadpal;
89 int vis;
90 char *old_locale;
91
92 /* fprintf(stderr, "Imlib Init\n");
93 fflush(stderr); */
94
95 palfile = NULL;
96 if (!disp)
97 {
98 fprintf(stderr, "IMLIB ERROR: no display\n");
99 return NULL;
100 }
101 vis = -1;
102 loadpal = 0;
103 id = (ImlibData *) malloc(sizeof(ImlibData));
104 if (!id)
105 {
106 fprintf(stderr, "IMLIB ERROR: Cannot alloc RAM for Initial data struct\n");
107 return NULL;
108 }
109 id->palette = NULL;
110 id->palette_orig = NULL;
111 id->fast_rgb = NULL;
112 id->fast_err = NULL;
113 id->fast_erg = NULL;
114 id->fast_erb = NULL;
115 id->x.disp = disp;
116 id->x.screen = DefaultScreen(disp); /* the screen number */
117 id->x.root = DefaultRootWindow(disp); /* the root window id */
118 id->x.visual = DefaultVisual(disp, id->x.screen); /* the visual type */
119 id->x.depth = DefaultDepth(disp, id->x.screen); /* the depth of the screen in bpp */
120
121 id->x.shm = 0;
122 id->x.shmp = 0;
123 id->max_shm = 0;
124 #ifdef HAVE_SHM
125 if (XShmQueryExtension(id->x.disp))
126 {
127 int maj, min, dum;
128 Bool pm;
129
130 if (XQueryExtension(id->x.disp, "MIT-SHM", &dum, &dum, &dum))
131 {
132 if (XShmQueryVersion(id->x.disp, &maj, &min, &pm) == True)
133 {
134 id->x.shm = 1;
135 id->x.shm_event = XShmGetEventBase(id->x.disp) + ShmCompletion;
136 id->x.last_xim = NULL;
137 id->x.last_sxim = NULL;
138 id->max_shm = 0x7fffffff;
139 if (pm == True && XShmPixmapFormat(id->x.disp) == ZPixmap)
140 id->x.shmp = 1;
141 }
142 }
143 }
144 #endif
145 id->cache.on_image = 0;
146 id->cache.size_image = 0;
147 id->cache.num_image = 0;
148 id->cache.used_image = 0;
149 id->cache.image = NULL;
150 id->cache.on_pixmap = 0;
151 id->cache.size_pixmap = 0;
152 id->cache.num_pixmap = 0;
153 id->cache.used_pixmap = 0;
154 id->cache.pixmap = NULL;
155 id->byte_order = 0;
156 id->fastrend = 0;
157 id->hiq = 0;
158 id->fallback = 1;
159 id->mod.gamma = 256;
160 id->mod.brightness = 256;
161 id->mod.contrast = 256;
162 id->rmod.gamma = 256;
163 id->rmod.brightness = 256;
164 id->rmod.contrast = 256;
165 id->gmod.gamma = 256;
166 id->gmod.brightness = 256;
167 id->gmod.contrast = 256;
168 id->bmod.gamma = 256;
169 id->bmod.brightness = 256;
170 id->bmod.contrast = 256;
171 id->ordered_dither = 1;
172
173 if (XGetWindowAttributes(disp, id->x.root, &xwa))
174 {
175 if (xwa.colormap)
176 id->x.root_cmap = xwa.colormap;
177 else
178 id->x.root_cmap = 0;
179 }
180 else
181 id->x.root_cmap = 0;
182 id->num_colors = 0;
183 s = xitk_asprintf("%s/.imrc", xine_get_homedir ());
184
185 old_locale = strdup(setlocale(LC_NUMERIC, NULL));
186 setlocale(LC_NUMERIC, "C");
187
188 #ifndef __EMX__
189 f = s ? fopen(s, "r") : NULL;
190 #else
191 f = s ? fopen(s, "rt") : NULL;
192 #endif
193
194 free(s);
195
196 if (!f)
197 {
198 #ifndef __EMX__
199 f = fopen(SYSTEM_IMRC, "r");
200 #else
201 f = fopen(__XOS2RedirRoot(SYSTEM_IMRC), "rt");
202 #endif
203 }
204
205 if (f)
206 {
207 size_t length;
208 s = NULL;
209 while (getline (&s, &length, f) >= 0)
210 {
211 const char *s2;
212
213 if (s[0] == '#')
214 continue;
215
216 s1 = strtok(s, " \t\n");
217
218 /* Blank line ? */
219
220 if (s1 == NULL)
221 continue;
222
223 s2 = strtok(NULL, " \t\n");
224 if (s2 == NULL)
225 s2 = ""; /* NULL argument */
226
227 if (!strcasecmp("PaletteFile", s1))
228 {
229 free(palfile);
230 palfile = strdup(s2);
231 }
232 else if (!strcasecmp("PaletteOverride", s1))
233 {
234 if (!strcasecmp("yes", s2))
235 override = 1;
236 else
237 override = 0;
238 }
239 else if (!strcasecmp("Dither", s1))
240 {
241 if (!strcasecmp("yes", s2))
242 dither = 1;
243 else
244 dither = 0;
245 }
246 else if (!strcasecmp("Remap", s1))
247 {
248 if (!strcasecmp("fast", s2))
249 remap = 1;
250 else
251 remap = 0;
252 }
253 else if (!strcasecmp("Mit-Shm", s1))
254 {
255 #ifdef HAVE_SHM
256 if (!strcasecmp("off", s2))
257 #endif
258 {
259 id->x.shm = 0;
260 id->x.shmp = 0;
261 }
262 }
263 else if (!strcasecmp("SharedPixmaps", s1))
264 {
265 #ifdef HAVE_SHM
266 if (!strcasecmp("off", s2))
267 #endif
268 id->x.shmp = 0;
269 }
270 else if (!strcasecmp("FastRender", s1))
271 {
272 if (!strcasecmp("on", s2))
273 id->fastrend = 1;
274 }
275 else if (!strcasecmp("HighQuality", s1))
276 {
277 if (!strcasecmp("on", s2))
278 id->hiq = 1;
279 }
280 else if (!strcasecmp("Shm_Max_Size", s1))
281 {
282 num = atoi(s2);
283 id->max_shm = num;
284 }
285 else if (!strcasecmp("Image_Cache_Size", s1))
286 {
287 num = atoi(s2);
288 id->cache.size_image = num;
289 }
290 else if (!strcasecmp("Pixmap_Cache_Size", s1))
291 {
292 num = atoi(s2);
293 id->cache.size_pixmap = num;
294 }
295 else if (!strcasecmp("Image_Cache", s1))
296 {
297 if (!strcasecmp("on", s2))
298 id->cache.on_image = 1;
299 }
300 else if (!strcasecmp("Pixmap_Cache", s1))
301 {
302 if (!strcasecmp("on", s2))
303 id->cache.on_pixmap = 1;
304 }
305 else if (!strcasecmp("ForceVisualID", s1))
306 {
307 unsigned int u;
308 sscanf(s, "%1024s %x", s1, &u);
309 vis = u;
310 }
311 else if (!strcasecmp("Fallback", s1))
312 {
313 if (!strcasecmp("off", s2))
314 id->fallback = 0;
315 else
316 id->fallback = 1;
317 }
318 else if (!strcasecmp("Gamma", s1))
319 {
320 id->mod.gamma = (int)(256.0 * atof(s2));
321 }
322 else if (!strcasecmp("Brightness", s1))
323 {
324 id->mod.brightness = (int)(256.0 * atof(s2));
325 }
326 else if (!strcasecmp("Contrast", s1))
327 {
328 id->mod.contrast = (int)(256.0 * atof(s2));
329 }
330 else if (!strcasecmp("Red_Gamma", s1))
331 {
332 id->rmod.gamma = (int)(256.0 * atof(s2));
333 }
334 else if (!strcasecmp("Red_Brightness", s1))
335 {
336 id->rmod.brightness = (int)(256.0 * atof(s2));
337 }
338 else if (!strcasecmp("Red_Contrast", s1))
339 {
340 id->rmod.contrast = (int)(256.0 * atof(s2));
341 }
342 else if (!strcasecmp("Green_Gamma", s1))
343 {
344 id->gmod.gamma = (int)(256.0 * atof(s2));
345 }
346 else if (!strcasecmp("Green_Brightness", s1))
347 {
348 id->gmod.brightness = (int)(256.0 * atof(s2));
349 }
350 else if (!strcasecmp("Green_Contrast", s1))
351 {
352 id->gmod.contrast = (int)(256.0 * atof(s2));
353 }
354 else if (!strcasecmp("Blue_Gamma", s1))
355 {
356 id->bmod.gamma = (int)(256.0 * atof(s2));
357 }
358 else if (!strcasecmp("Blue_Brightness", s1))
359 {
360 id->bmod.brightness = (int)(256.0 * atof(s2));
361 }
362 else if (!strcasecmp("Blue_Contrast", s1))
363 {
364 id->bmod.contrast = (int)(256.0 * atof(s2));
365 }
366 else if (!strcasecmp("Ordered_Dither", s1))
367 {
368 if (!strcasecmp("off", s2))
369 id->ordered_dither = 0;
370 else
371 id->ordered_dither = 1;
372 }
373 }
374 free(s);
375 fclose(f);
376 }
377 setlocale(LC_NUMERIC, old_locale);
378 free(old_locale);
379
380 /* list all visuals for the default screen */
381 xvi.screen = id->x.screen;
382 xvir = XGetVisualInfo(disp, VisualScreenMask, &xvi, &num);
383 if (vis >= 0)
384 {
385 /* use the forced visual id */
386 maxn = 0;
387 for (i = 0; i < num; i++)
388 {
389 if (xvir[i].visualid == (VisualID) vis)
390 maxn = i;
391 }
392 if (maxn >= 0)
393 {
394 unsigned long rmsk, gmsk, bmsk;
395
396 id->x.depth = xvir[maxn].depth;
397 id->x.visual = xvir[maxn].visual;
398 rmsk = xvir[maxn].red_mask;
399 gmsk = xvir[maxn].green_mask;
400 bmsk = xvir[maxn].blue_mask;
401
402 if ((rmsk > gmsk) && (gmsk > bmsk))
403 id->byte_order = BYTE_ORD_24_RGB;
404 else if ((rmsk > bmsk) && (bmsk > gmsk))
405 id->byte_order = BYTE_ORD_24_RBG;
406 else if ((bmsk > rmsk) && (rmsk > gmsk))
407 id->byte_order = BYTE_ORD_24_BRG;
408 else if ((bmsk > gmsk) && (gmsk > rmsk))
409 id->byte_order = BYTE_ORD_24_BGR;
410 else if ((gmsk > rmsk) && (rmsk > bmsk))
411 id->byte_order = BYTE_ORD_24_GRB;
412 else if ((gmsk > bmsk) && (bmsk > rmsk))
413 id->byte_order = BYTE_ORD_24_GBR;
414 else
415 id->byte_order = 0;
416 }
417 else
418 fprintf(stderr, "IMLIB ERROR: Visual Id no 0x%x specified in the imrc file is invalid on this display.\nUsing Default Visual.\n", vis);
419 }
420 else
421 {
422 if (xvir)
423 {
424 /* find the highest bit-depth supported by visuals */
425 max = 0;
426 for (i = 0; i < num; i++)
427 {
428 if (xvir[i].depth > max)
429 max = xvir[i].depth;
430 }
431 if (max > 8)
432 {
433 id->x.depth = max;
434 clas = -1;
435 maxn = -1;
436 for (i = 0; i < num; i++)
437 {
438 if (xvir[i].depth == id->x.depth)
439 {
440 if ((xvir[i].class > clas) &&
441 (xvir[i].class != DirectColor))
442 {
443 maxn = i;
444 clas = xvir[i].class;
445 }
446 }
447 }
448 if (maxn >= 0)
449 {
450 unsigned long rmsk, gmsk, bmsk;
451
452 id->x.visual = xvir[maxn].visual;
453 rmsk = xvir[maxn].red_mask;
454 gmsk = xvir[maxn].green_mask;
455 bmsk = xvir[maxn].blue_mask;
456
457 if ((rmsk > gmsk) && (gmsk > bmsk))
458 id->byte_order = BYTE_ORD_24_RGB;
459 else if ((rmsk > bmsk) && (bmsk > gmsk))
460 id->byte_order = BYTE_ORD_24_RBG;
461 else if ((bmsk > rmsk) && (rmsk > gmsk))
462 id->byte_order = BYTE_ORD_24_BRG;
463 else if ((bmsk > gmsk) && (gmsk > rmsk))
464 id->byte_order = BYTE_ORD_24_BGR;
465 else if ((gmsk > rmsk) && (rmsk > bmsk))
466 id->byte_order = BYTE_ORD_24_GRB;
467 else if ((gmsk > bmsk) && (bmsk > rmsk))
468 id->byte_order = BYTE_ORD_24_GBR;
469 else
470 id->byte_order = 0;
471 }
472 }
473 }
474 }
475 id->x.render_depth = id->x.depth;
476 XFree(xvir);
477 if (id->x.depth == 16)
478 {
479 xvi.visual = id->x.visual;
480 xvi.visualid = XVisualIDFromVisual(id->x.visual);
481 xvir = XGetVisualInfo(disp, VisualIDMask, &xvi, &num);
482 if (xvir)
483 {
484 if (xvir->red_mask != 0xf800)
485 id->x.render_depth = 15;
486 XFree(xvir);
487 }
488 }
489 if (id->x.depth < 8)
490 id->x.shmp = 0;
491 if ((id->x.depth <= 8) || (override == 1))
492 loadpal = 1;
493 if (loadpal)
494 {
495 if (dither == 1)
496 {
497 if (remap == 1)
498 id->render_type = RT_DITHER_PALETTE_FAST;
499 else
500 id->render_type = RT_DITHER_PALETTE;
501 }
502 else
503 {
504 if (remap == 1)
505 id->render_type = RT_PLAIN_PALETTE_FAST;
506 else
507 id->render_type = RT_PLAIN_PALETTE;
508 }
509 if (palfile != NULL)
510 Imlib_load_colors(id, palfile);
511 if (id->num_colors == 0)
512 {
513 fprintf(stderr, "IMLIB ERROR: Cannot Find Palette. A Palette is required for this mode\n");
514 Imlib_load_default_colors (id);
515 }
516 }
517 else
518 {
519 if (id->hiq == 1)
520 id->render_type = RT_DITHER_TRUECOL;
521 else
522 id->render_type = RT_PLAIN_TRUECOL;
523 }
524 {
525 XSetWindowAttributes at;
526 unsigned long mask;
527
528 at.border_pixel = 0;
529 at.backing_store = NotUseful;
530 at.background_pixel = 0;
531 at.save_under = False;
532 at.override_redirect = True;
533 mask = CWOverrideRedirect | CWBackPixel | CWBorderPixel |
534 CWBackingStore | CWSaveUnder;
535 if (id->x.visual != DefaultVisual(disp, id->x.screen))
536 {
537 Colormap cm;
538
539 cm = XCreateColormap(id->x.disp, id->x.root,
540 id->x.visual, AllocNone);
541 if (cm)
542 {
543 mask |= CWColormap;
544 id->x.root_cmap = cm;
545 at.colormap = cm;
546 }
547 }
548 id->x.base_window = XCreateWindow(id->x.disp, id->x.root,
549 -100, -100, 10, 10, 0,
550 id->x.depth, InputOutput,
551 id->x.visual, mask, &at);
552 }
553 {
554 /* Turn off fastrender if there is an endianess diff between */
555 /* client and Xserver */
556 int byt, bit;
557
558 byt = ImageByteOrder(id->x.disp); /* LSBFirst | MSBFirst */
559 bit = BitmapBitOrder(id->x.disp); /* LSBFirst | MSBFirst */
560 id->x.byte_order = byt;
561 id->x.bit_order = bit;
562 /* if little endian && server big */
563 if ((htonl(1) != 1) && (byt == MSBFirst))
564 id->fastrend = 0;
565 /* if big endian && server little */
566 if ((htonl(1) == 1) && (byt == LSBFirst))
567 id->fastrend = 0;
568 }
569 free(palfile);
570
571 #ifdef HAVE_SHM
572 if (id->x.shm)
573 {
574 XImage *xim;
575
576 xim = XShmCreateImage(id->x.disp, id->x.visual, id->x.depth,
577 ZPixmap, NULL, &id->x.last_shminfo, 10, 10);
578 if (!xim)
579 {
580 id->x.shm = 0;
581 id->x.shmp = 0;
582 }
583 else
584 {
585 id->x.last_shminfo.shmid =
586 shmget(IPC_PRIVATE, xim->bytes_per_line * xim->height,
587 IPC_CREAT | 0777);
588 if (id->x.last_shminfo.shmid < 0)
589 {
590 XDestroyImage(xim);
591 id->x.shm = 0;
592 id->x.shmp = 0;
593 }
594 else
595 {
596 id->x.last_shminfo.shmaddr = xim->data =
597 shmat(id->x.last_shminfo.shmid, 0, 0);
598 if (id->x.last_shminfo.shmaddr == (char *)-1)
599 {
600 XDestroyImage(xim);
601 shmctl(id->x.last_shminfo.shmid, IPC_RMID, 0);
602 id->x.shm = 0;
603 id->x.shmp = 0;
604 }
605 else
606 {
607 id->x.last_shminfo.readOnly = False;
608 XSetErrorHandler((XErrorHandler) HandleXError);
609 x_error = 0;
610 XShmAttach(id->x.disp, &id->x.last_shminfo);
611 XSync(disp, False);
612 if (x_error)
613 {
614 id->x.shm = 0;
615 id->x.shmp = 0;
616 }
617 else
618 XShmDetach(id->x.disp, &id->x.last_shminfo);
619 XDestroyImage(xim);
620 shmdt(id->x.last_shminfo.shmaddr);
621 shmctl(id->x.last_shminfo.shmid, IPC_RMID, 0);
622 }
623 }
624 }
625 }
626 #endif
627
628 return id;
629 }
630
631 ImlibData *
Imlib_init_with_params(Display * disp,ImlibInitParams * p)632 Imlib_init_with_params(Display * disp, ImlibInitParams * p)
633 {
634 ImlibData *id;
635 XWindowAttributes xwa;
636 XVisualInfo xvi, *xvir;
637 char *s, *s1;
638 FILE *f;
639 int override = 0;
640 int dither = 0;
641 int remap = 1;
642 int num;
643 int i, max, maxn;
644 int clas;
645 char *palfile;
646 int loadpal;
647 int vis;
648 int newcm = 0;
649 char *old_locale;
650
651 palfile = NULL;
652 if (!disp)
653 {
654 fprintf(stderr, "IMLIB ERROR: no display\n");
655 return NULL;
656 }
657 vis = -1;
658 loadpal = 0;
659 id = (ImlibData *) malloc(sizeof(ImlibData));
660 if (!id)
661 {
662 fprintf(stderr, "IMLIB ERROR: Cannot alloc RAM for Initial data struct\n");
663 return NULL;
664 }
665 id->palette = NULL;
666 id->palette_orig = NULL;
667 id->fast_rgb = NULL;
668 id->fast_err = NULL;
669 id->fast_erg = NULL;
670 id->fast_erb = NULL;
671 id->x.disp = disp;
672 id->x.screen = DefaultScreen(disp); /* the screen number */
673 id->x.root = DefaultRootWindow(disp); /* the root window id */
674 id->x.visual = DefaultVisual(disp, id->x.screen); /* the visual type */
675 id->x.depth = DefaultDepth(disp, id->x.screen); /* the depth of the screen in bpp */
676
677 id->x.shm = 0;
678 id->x.shmp = 0;
679 id->max_shm = 0;
680 #ifdef HAVE_SHM
681 if (XShmQueryExtension(id->x.disp))
682 {
683 int maj, min, dum;
684 Bool pm;
685
686 if (XQueryExtension(id->x.disp, "MIT-SHM", &dum, &dum, &dum))
687 {
688 if (XShmQueryVersion(id->x.disp, &maj, &min, &pm) == True)
689 {
690 id->x.shm = 1;
691 id->x.shm_event = XShmGetEventBase(id->x.disp) + ShmCompletion;
692 id->x.last_xim = NULL;
693 id->x.last_sxim = NULL;
694 id->max_shm = 0x7fffffff;
695 if (pm == True && XShmPixmapFormat(id->x.disp) == ZPixmap)
696 id->x.shmp = 1;
697 }
698 }
699 }
700 #endif
701 id->cache.on_image = 0;
702 id->cache.size_image = 0;
703 id->cache.num_image = 0;
704 id->cache.used_image = 0;
705 id->cache.image = NULL;
706 id->cache.on_pixmap = 0;
707 id->cache.size_pixmap = 0;
708 id->cache.num_pixmap = 0;
709 id->cache.used_pixmap = 0;
710 id->cache.pixmap = NULL;
711 id->byte_order = 0;
712 id->fastrend = 0;
713 id->hiq = 0;
714 id->fallback = 1;
715 id->mod.gamma = 256;
716 id->mod.brightness = 256;
717 id->mod.contrast = 256;
718 id->rmod.gamma = 256;
719 id->rmod.brightness = 256;
720 id->rmod.contrast = 256;
721 id->gmod.gamma = 256;
722 id->gmod.brightness = 256;
723 id->gmod.contrast = 256;
724 id->bmod.gamma = 256;
725 id->bmod.brightness = 256;
726 id->bmod.contrast = 256;
727 id->ordered_dither = 1;
728
729 if (XGetWindowAttributes(disp, id->x.root, &xwa))
730 {
731 if (xwa.colormap)
732 id->x.root_cmap = xwa.colormap;
733 else
734 id->x.root_cmap = 0;
735 }
736 else
737 id->x.root_cmap = 0;
738 id->num_colors = 0;
739 s = xitk_asprintf("%s/.imrc", xine_get_homedir ());
740
741 old_locale = strdup(setlocale(LC_NUMERIC, NULL));
742 setlocale(LC_NUMERIC, "C");
743
744 #ifndef __EMX__
745 f = s ? fopen(s, "r") : NULL;
746 #else
747 f = s ? fopen(s, "rt") : NULL;
748 #endif
749
750 free(s);
751
752 if (!f)
753 {
754 #ifndef __EMX__
755 f = fopen(SYSTEM_IMRC, "r");
756 #else
757 f = fopen(__XOS2RedirRoot(SYSTEM_IMRC), "rt");
758 #endif
759 }
760
761 if (f)
762 {
763 size_t length;
764 s = NULL;
765 while (getline (&s, &length, f) >= 0)
766 {
767 const char *s2;
768
769 if (s[0] == '#')
770 continue;
771
772 s1 = strtok(s, " \t\n");
773
774 /* Blank line ? */
775
776 if (s1 == NULL)
777 continue;
778
779 s2 = strtok(NULL, " \t\n");
780 if (s2 == NULL)
781 s2 = ""; /* NULL argument */
782
783 if (!strcasecmp("PaletteFile", s1))
784 {
785 free(palfile);
786 palfile = strdup(s2);
787 }
788 else if (!strcasecmp("PaletteOverride", s1))
789 {
790 if (!strcasecmp("yes", s2))
791 override = 1;
792 else
793 override = 0;
794 }
795 else if (!strcasecmp("Dither", s1))
796 {
797 if (!strcasecmp("yes", s2))
798 dither = 1;
799 else
800 dither = 0;
801 }
802 else if (!strcasecmp("Remap", s1))
803 {
804 if (!strcasecmp("fast", s2))
805 remap = 1;
806 else
807 remap = 0;
808 }
809 else if (!strcasecmp("Mit-Shm", s1))
810 {
811 #ifdef HAVE_SHM
812 if (!strcasecmp("off", s2))
813 #endif
814 {
815 id->x.shm = 0;
816 id->x.shmp = 0;
817 }
818 }
819 else if (!strcasecmp("SharedPixmaps", s1))
820 {
821 #ifdef HAVE_SHM
822 if (!strcasecmp("off", s2))
823 #endif
824 id->x.shmp = 0;
825 }
826 else if (!strcasecmp("FastRender", s1))
827 {
828 if (!strcasecmp("on", s2))
829 id->fastrend = 1;
830 }
831 else if (!strcasecmp("HighQuality", s1))
832 {
833 if (!strcasecmp("on", s2))
834 id->hiq = 1;
835 }
836 else if (!strcasecmp("Shm_Max_Size", s1))
837 {
838 num = atoi(s2);
839 id->max_shm = num;
840 }
841 else if (!strcasecmp("Image_Cache_Size", s1))
842 {
843 num = atoi(s2);
844 id->cache.size_image = num;
845 }
846 else if (!strcasecmp("Pixmap_Cache_Size", s1))
847 {
848 num = atoi(s2);
849 id->cache.size_pixmap = num;
850 }
851 else if (!strcasecmp("Image_Cache", s1))
852 {
853 if (!strcasecmp("on", s2))
854 id->cache.on_image = 1;
855 }
856 else if (!strcasecmp("Pixmap_Cache", s1))
857 {
858 if (!strcasecmp("on", s2))
859 id->cache.on_pixmap = 1;
860 }
861 else if (!strcasecmp("ForceVisualID", s1))
862 {
863 unsigned int u;
864 sscanf(s, "%1024s %x", s1, &u);
865 vis = u;
866 }
867 else if (!strcasecmp("Fallback", s1))
868 {
869 if (!strcasecmp("off", s2))
870 id->fallback = 0;
871 else
872 id->fallback = 1;
873 }
874 else if (!strcasecmp("Gamma", s1))
875 {
876 id->mod.gamma = (int)(256.0 * atof(s2));
877 }
878 else if (!strcasecmp("Brightness", s1))
879 {
880 id->mod.brightness = (int)(256.0 * atof(s2));
881 }
882 else if (!strcasecmp("Contrast", s1))
883 {
884 id->mod.contrast = (int)(256.0 * atof(s2));
885 }
886 else if (!strcasecmp("Red_Gamma", s1))
887 {
888 id->rmod.gamma = (int)(256.0 * atof(s2));
889 }
890 else if (!strcasecmp("Red_Brightness", s1))
891 {
892 id->rmod.brightness = (int)(256.0 * atof(s2));
893 }
894 else if (!strcasecmp("Red_Contrast", s1))
895 {
896 id->rmod.contrast = (int)(256.0 * atof(s2));
897 }
898 else if (!strcasecmp("Green_Gamma", s1))
899 {
900 id->gmod.gamma = (int)(256.0 * atof(s2));
901 }
902 else if (!strcasecmp("Green_Brightness", s1))
903 {
904 id->gmod.brightness = (int)(256.0 * atof(s2));
905 }
906 else if (!strcasecmp("Green_Contrast", s1))
907 {
908 id->gmod.contrast = (int)(256.0 * atof(s2));
909 }
910 else if (!strcasecmp("Blue_Gamma", s1))
911 {
912 id->bmod.gamma = (int)(256.0 * atof(s2));
913 }
914 else if (!strcasecmp("Blue_Brightness", s1))
915 {
916 id->bmod.brightness = (int)(256.0 * atof(s2));
917 }
918 else if (!strcasecmp("Blue_Contrast", s1))
919 {
920 id->bmod.contrast = (int)(256.0 * atof(s2));
921 }
922 else if (!strcasecmp("Ordered_Dither", s1))
923 {
924 if (!strcasecmp("off", s2))
925 id->ordered_dither = 0;
926 else
927 id->ordered_dither = 1;
928 }
929 }
930 free (s);
931 fclose(f);
932 }
933 setlocale(LC_NUMERIC, old_locale);
934 free(old_locale);
935
936 if (p)
937 {
938 if (p->flags & PARAMS_VISUALID)
939 vis = p->visualid;
940 if (p->flags & PARAMS_PALETTEFILE)
941 strcpy(palfile, p->palettefile);
942 if (p->flags & PARAMS_SHAREDMEM)
943 {
944 if (!p->sharedmem)
945 {
946 id->x.shm = 0;
947 id->x.shmp = 0;
948 }
949 else
950 {
951 id->x.shm = 1;
952 id->x.shmp = 0;
953 }
954 }
955 if (p->flags & PARAMS_SHAREDPIXMAPS)
956 {
957 if (id->x.shm)
958 id->x.shmp = p->sharedpixmaps;
959 }
960 if (p->flags & PARAMS_PALETTEOVERRIDE)
961 override = p->paletteoverride;
962 if (p->flags & PARAMS_REMAP)
963 remap = p->remap;
964 if (p->flags & PARAMS_FASTRENDER)
965 id->fastrend = p->fastrender;
966 if (p->flags & PARAMS_HIQUALITY)
967 id->hiq = p->hiquality;
968 if (p->flags & PARAMS_DITHER)
969 dither = p->dither;
970 if (p->flags & PARAMS_IMAGECACHESIZE)
971 id->cache.size_image = p->imagecachesize;
972 if (p->flags & PARAMS_PIXMAPCACHESIZE)
973 id->cache.size_pixmap = p->pixmapcachesize;
974 if (p->flags & PARAMS_COLORMAP)
975 {
976 id->x.root_cmap = p->cmap;
977 newcm = 1;
978 }
979 }
980 /* list all visuals for the default screen */
981 xvi.screen = id->x.screen;
982 xvir = XGetVisualInfo(disp, VisualScreenMask, &xvi, &num);
983 if (vis >= 0)
984 {
985 /* use the forced visual id */
986 maxn = 0;
987 for (i = 0; i < num; i++)
988 {
989 if (xvir[i].visualid == (VisualID) vis)
990 maxn = i;
991 }
992 if (maxn >= 0)
993 {
994 unsigned long rmsk, gmsk, bmsk;
995
996 id->x.depth = xvir[maxn].depth;
997 id->x.visual = xvir[maxn].visual;
998 rmsk = xvir[maxn].red_mask;
999 gmsk = xvir[maxn].green_mask;
1000 bmsk = xvir[maxn].blue_mask;
1001
1002 if ((rmsk > gmsk) && (gmsk > bmsk))
1003 id->byte_order = BYTE_ORD_24_RGB;
1004 else if ((rmsk > bmsk) && (bmsk > gmsk))
1005 id->byte_order = BYTE_ORD_24_RBG;
1006 else if ((bmsk > rmsk) && (rmsk > gmsk))
1007 id->byte_order = BYTE_ORD_24_BRG;
1008 else if ((bmsk > gmsk) && (gmsk > rmsk))
1009 id->byte_order = BYTE_ORD_24_BGR;
1010 else if ((gmsk > rmsk) && (rmsk > bmsk))
1011 id->byte_order = BYTE_ORD_24_GRB;
1012 else if ((gmsk > bmsk) && (bmsk > rmsk))
1013 id->byte_order = BYTE_ORD_24_GBR;
1014 else
1015 id->byte_order = 0;
1016 }
1017 else
1018 fprintf(stderr, "IMLIB ERROR: Visual Id no 0x%x specified in the imrc file is invalid on this display.\nUsing Default Visual.\n", vis);
1019 }
1020 else
1021 {
1022 if (xvir)
1023 {
1024 /* find the highest bit-depth supported by visuals */
1025 max = 0;
1026 for (i = 0; i < num; i++)
1027 {
1028 if (xvir[i].depth > max)
1029 max = xvir[i].depth;
1030 }
1031 if (max > 8)
1032 {
1033 id->x.depth = max;
1034 clas = -1;
1035 maxn = -1;
1036 for (i = 0; i < num; i++)
1037 {
1038 if (xvir[i].depth == id->x.depth)
1039 {
1040 if ((xvir[i].class > clas) && (xvir[i].class != DirectColor))
1041 {
1042 maxn = i;
1043 clas = xvir[i].class;
1044 }
1045 }
1046 }
1047 if (maxn >= 0)
1048 {
1049 unsigned long rmsk, gmsk, bmsk;
1050
1051 id->x.visual = xvir[maxn].visual;
1052 rmsk = xvir[maxn].red_mask;
1053 gmsk = xvir[maxn].green_mask;
1054 bmsk = xvir[maxn].blue_mask;
1055
1056 if ((rmsk > gmsk) && (gmsk > bmsk))
1057 id->byte_order = BYTE_ORD_24_RGB;
1058 else if ((rmsk > bmsk) && (bmsk > gmsk))
1059 id->byte_order = BYTE_ORD_24_RBG;
1060 else if ((bmsk > rmsk) && (rmsk > gmsk))
1061 id->byte_order = BYTE_ORD_24_BRG;
1062 else if ((bmsk > gmsk) && (gmsk > rmsk))
1063 id->byte_order = BYTE_ORD_24_BGR;
1064 else if ((gmsk > rmsk) && (rmsk > bmsk))
1065 id->byte_order = BYTE_ORD_24_GRB;
1066 else if ((gmsk > bmsk) && (bmsk > rmsk))
1067 id->byte_order = BYTE_ORD_24_GBR;
1068 else
1069 id->byte_order = 0;
1070 }
1071 }
1072 }
1073 }
1074 id->x.render_depth = id->x.depth;
1075 XFree(xvir);
1076 if (id->x.depth == 16)
1077 {
1078 xvi.visual = id->x.visual;
1079 xvi.visualid = XVisualIDFromVisual(id->x.visual);
1080 xvir = XGetVisualInfo(disp, VisualIDMask, &xvi, &num);
1081 if (xvir)
1082 {
1083 if (xvir->red_mask != 0xf800)
1084 id->x.render_depth = 15;
1085 XFree(xvir);
1086 }
1087 }
1088 if (id->x.depth < 8)
1089 id->x.shmp = 0;
1090 if ((id->x.depth <= 8) || (override == 1))
1091 loadpal = 1;
1092 if (loadpal)
1093 {
1094 if (dither == 1)
1095 {
1096 if (remap == 1)
1097 id->render_type = RT_DITHER_PALETTE_FAST;
1098 else
1099 id->render_type = RT_DITHER_PALETTE;
1100 }
1101 else
1102 {
1103 if (remap == 1)
1104 id->render_type = RT_PLAIN_PALETTE_FAST;
1105 else
1106 id->render_type = RT_PLAIN_PALETTE;
1107 }
1108 if (palfile != NULL)
1109 Imlib_load_colors(id, palfile);
1110 if (id->num_colors == 0)
1111 {
1112 fprintf(stderr, "IMLIB ERROR: Cannot Find Palette. Using built-in palette.\n");
1113 Imlib_load_default_colors (id);
1114 }
1115 }
1116 else
1117 {
1118 if (id->hiq == 1)
1119 id->render_type = RT_DITHER_TRUECOL;
1120 else
1121 id->render_type = RT_PLAIN_TRUECOL;
1122 }
1123 {
1124 XSetWindowAttributes at;
1125 unsigned long mask;
1126
1127 at.border_pixel = 0;
1128 at.backing_store = NotUseful;
1129 at.background_pixel = 0;
1130 at.save_under = False;
1131 at.override_redirect = True;
1132 mask = CWOverrideRedirect | CWBackPixel | CWBorderPixel |
1133 CWBackingStore | CWSaveUnder;
1134 if (id->x.visual != DefaultVisual(disp, id->x.screen))
1135 {
1136 Colormap cm;
1137
1138 cm = XCreateColormap(id->x.disp, id->x.root,
1139 id->x.visual, AllocNone);
1140 if (cm)
1141 {
1142 mask |= CWColormap;
1143 id->x.root_cmap = cm;
1144 at.colormap = cm;
1145 newcm = 1;
1146 }
1147 }
1148 else if (newcm)
1149 {
1150 mask |= CWColormap;
1151 at.colormap = id->x.root_cmap;
1152 }
1153 id->x.base_window = XCreateWindow(id->x.disp, id->x.root,
1154 -100, -100, 10, 10, 0,
1155 id->x.depth, InputOutput,
1156 id->x.visual, mask, &at);
1157 }
1158 {
1159 /* Turn off fastrender if there is an endianess diff between */
1160 /* client and Xserver */
1161 int byt;
1162
1163 byt = ImageByteOrder(id->x.disp); /* LSBFirst | MSBFirst */
1164 /* if little endian && server big */
1165 if ((htonl(1) != 1) && (byt == MSBFirst))
1166 id->fastrend = 0;
1167 /* if big endian && server little */
1168 if ((htonl(1) == 1) && (byt == LSBFirst))
1169 id->fastrend = 0;
1170 }
1171 free(palfile);
1172 #ifdef HAVE_SHM
1173 if (id->x.shm)
1174 {
1175 XImage *xim;
1176
1177 xim = XShmCreateImage(id->x.disp, id->x.visual, id->x.depth,
1178 ZPixmap, NULL, &id->x.last_shminfo, 10, 10);
1179 if (!xim)
1180 {
1181 id->x.shm = 0;
1182 id->x.shmp = 0;
1183 }
1184 else
1185 {
1186 id->x.last_shminfo.shmid =
1187 shmget(IPC_PRIVATE, xim->bytes_per_line * xim->height,
1188 IPC_CREAT | 0777);
1189 if (id->x.last_shminfo.shmid < 0)
1190 {
1191 XDestroyImage(xim);
1192 id->x.shm = 0;
1193 id->x.shmp = 0;
1194 }
1195 else
1196 {
1197 id->x.last_shminfo.shmaddr = xim->data =
1198 shmat(id->x.last_shminfo.shmid, 0, 0);
1199 if (id->x.last_shminfo.shmaddr == (char *)-1)
1200 {
1201 XDestroyImage(xim);
1202 shmctl(id->x.last_shminfo.shmid, IPC_RMID, 0);
1203 id->x.shm = 0;
1204 id->x.shmp = 0;
1205 }
1206 else
1207 {
1208 id->x.last_shminfo.readOnly = False;
1209 XSetErrorHandler((XErrorHandler) HandleXError);
1210 x_error = 0;
1211 XShmAttach(id->x.disp, &id->x.last_shminfo);
1212 XSync(disp, False);
1213 if (x_error)
1214 {
1215 id->x.shm = 0;
1216 id->x.shmp = 0;
1217 }
1218 else
1219 XShmDetach(id->x.disp, &id->x.last_shminfo);
1220 XDestroyImage(xim);
1221 shmdt(id->x.last_shminfo.shmaddr);
1222 shmctl(id->x.last_shminfo.shmid, IPC_RMID, 0);
1223 }
1224 }
1225 }
1226 }
1227 #endif
1228 return id;
1229 }
1230
1231 Pixmap
Imlib_copy_image(ImlibData * id,ImlibImage * im)1232 Imlib_copy_image(ImlibData * id, ImlibImage * im)
1233 {
1234 Pixmap p;
1235 GC tgc;
1236 XGCValues gcv;
1237
1238 if (!im || !im->pixmap)
1239 return 0;
1240 p = XCreatePixmap(id->x.disp, id->x.base_window, im->width, im->height, id->x.depth);
1241 gcv.graphics_exposures = False;
1242 tgc = XCreateGC(id->x.disp, p, GCGraphicsExposures, &gcv);
1243 XCopyArea(id->x.disp, im->pixmap, p, tgc, 0, 0, im->width, im->height, 0, 0);
1244 XFreeGC(id->x.disp, tgc);
1245 return p;
1246 }
1247
1248 Pixmap
Imlib_move_image(ImlibData * id,ImlibImage * im)1249 Imlib_move_image(ImlibData * id, ImlibImage * im)
1250 {
1251 Pixmap p;
1252
1253 if (!im)
1254 return 0;
1255 p = im->pixmap;
1256 im->pixmap = 0;
1257 return p;
1258 }
1259
1260 Pixmap
Imlib_copy_mask(ImlibData * id,ImlibImage * im)1261 Imlib_copy_mask(ImlibData * id, ImlibImage * im)
1262 {
1263 Pixmap p;
1264 GC tgc;
1265 XGCValues gcv;
1266
1267 if (!im || !im->shape_mask)
1268 return 0;
1269 p = XCreatePixmap(id->x.disp, id->x.base_window, im->width, im->height, 1);
1270 gcv.graphics_exposures = False;
1271 tgc = XCreateGC(id->x.disp, p, GCGraphicsExposures, &gcv);
1272 XCopyArea(id->x.disp, im->shape_mask, p, tgc, 0, 0, im->width, im->height, 0, 0);
1273 XFreeGC(id->x.disp, tgc);
1274 return p;
1275 }
1276
1277 Pixmap
Imlib_move_mask(ImlibData * id,ImlibImage * im)1278 Imlib_move_mask(ImlibData * id, ImlibImage * im)
1279 {
1280 Pixmap p;
1281
1282 if (!im)
1283 return 0;
1284 p = im->shape_mask;
1285 im->shape_mask = 0;
1286 return p;
1287 }
1288
1289 void
Imlib_destroy_image(ImlibData * id,ImlibImage * im)1290 Imlib_destroy_image(ImlibData * id, ImlibImage * im)
1291 {
1292 if (im)
1293 {
1294 if (id->cache.on_image)
1295 {
1296 free_image(id, im);
1297 clean_caches(id);
1298 }
1299 else
1300 nullify_image(id, im);
1301 }
1302 }
1303
1304 void
Imlib_kill_image(ImlibData * id,ImlibImage * im)1305 Imlib_kill_image(ImlibData * id, ImlibImage * im)
1306 {
1307 if (im)
1308 {
1309 if (id->cache.on_image)
1310 {
1311 flush_image(id, im);
1312 free_image(id, im);
1313 clean_caches(id);
1314 }
1315 else
1316 nullify_image(id, im);
1317 }
1318 }
1319
1320 void
Imlib_free_pixmap(ImlibData * id,Pixmap pmap)1321 Imlib_free_pixmap(ImlibData * id, Pixmap pmap)
1322 {
1323 if (pmap)
1324 {
1325 free_pixmappmap(id, pmap);
1326 clean_caches(id);
1327 }
1328 }
1329
1330 void
Imlib_set_image_border(ImlibData * id,ImlibImage * im,ImlibBorder * border)1331 Imlib_set_image_border(ImlibData * id, ImlibImage * im, ImlibBorder * border)
1332 {
1333 if ((im) && (border))
1334 {
1335 if ((im->border.left != border->left) ||
1336 (im->border.right != border->right) ||
1337 (im->border.top != border->top) ||
1338 (im->border.bottom != border->bottom))
1339 {
1340 dirty_pixmaps(id, im);
1341
1342 im->border.left = border->left;
1343 im->border.right = border->right;
1344 im->border.top = border->top;
1345 im->border.bottom = border->bottom;
1346 }
1347 }
1348 }
1349
1350 void
Imlib_get_image_border(ImlibData * id,ImlibImage * im,ImlibBorder * border)1351 Imlib_get_image_border(ImlibData * id, ImlibImage * im, ImlibBorder * border)
1352 {
1353 if ((im) && (border))
1354 {
1355 border->left = im->border.left;
1356 border->right = im->border.right;
1357 border->top = im->border.top;
1358 border->bottom = im->border.bottom;
1359 }
1360 }
1361
1362 void
Imlib_get_image_shape(ImlibData * id,ImlibImage * im,ImlibColor * color)1363 Imlib_get_image_shape(ImlibData * id, ImlibImage * im, ImlibColor * color)
1364 {
1365 if ((!im) || (!color))
1366 return;
1367
1368 color->r = im->shape_color.r;
1369 color->g = im->shape_color.g;
1370 color->b = im->shape_color.b;
1371 }
1372
1373 void
Imlib_set_image_shape(ImlibData * id,ImlibImage * im,ImlibColor * color)1374 Imlib_set_image_shape(ImlibData * id, ImlibImage * im, ImlibColor * color)
1375 {
1376 if ((!im) || (!color))
1377 return;
1378 if ((im->shape_color.r != color->r) || (im->shape_color.g != color->g) || (im->shape_color.b != color->b))
1379 {
1380 im->shape_color.r = color->r;
1381 im->shape_color.g = color->g;
1382 im->shape_color.b = color->b;
1383 dirty_pixmaps(id, im);
1384 }
1385 }
1386
1387 int
Imlib_get_fallback(ImlibData * id)1388 Imlib_get_fallback(ImlibData * id)
1389 {
1390 if (!id)
1391 return 0;
1392 return id->fallback;
1393 }
1394
1395 void
Imlib_set_fallback(ImlibData * id,int fallback)1396 Imlib_set_fallback(ImlibData * id, int fallback)
1397 {
1398 if (!id)
1399 return;
1400 id->fallback = fallback;
1401 }
1402
1403 Visual *
Imlib_get_visual(ImlibData * id)1404 Imlib_get_visual(ImlibData * id)
1405 {
1406 if (!id)
1407 return NULL;
1408 return id->x.visual;
1409 }
1410
1411 Colormap
Imlib_get_colormap(ImlibData * id)1412 Imlib_get_colormap(ImlibData * id)
1413 {
1414 if (!id)
1415 return 0;
1416 return id->x.root_cmap;
1417 }
1418
1419 char *
Imlib_get_sysconfig(ImlibData * id)1420 Imlib_get_sysconfig(ImlibData * id)
1421 {
1422 #ifndef __EMX__
1423 return strdup(SYSTEM_IMRC);
1424 #else
1425 return strdup(__XOS2RedirRoot(SYSTEM_IMRC));
1426 #endif
1427 }
1428