~ubuntu-branches/ubuntu/lucid/xli/lucid

« back to all changes in this revision

Viewing changes to ddxli.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2005-03-18 12:46:39 UTC
  • Revision ID: james.westby@ubuntu.com-20050318124639-sbi2ud4ee08pqjer
Tags: upstream-1.17.0
ImportĀ upstreamĀ versionĀ 1.17.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * General Device dependent code for xli.
 
3
 *
 
4
 * Author; Graeme W. Gill
 
5
 */
 
6
 
 
7
#include "xli.h"
 
8
#include <ctype.h>
 
9
 
 
10
static int saParseXColor(DisplayInfo *dinfo, char *spec, XColor *xcolor);
 
11
 
 
12
/* Initialise a display info structure to default values */
 
13
void xliDefaultDispinfo(DisplayInfo *dinfo)
 
14
{
 
15
        dinfo->width = 0;
 
16
        dinfo->height = 0;
 
17
        dinfo->disp = NULL;
 
18
        dinfo->scrn = 0;
 
19
        dinfo->colormap = 0;
 
20
}
 
21
 
 
22
/* open up a display and screen, and stick the info away */
 
23
/* Return FALSE on failure */
 
24
boolean xliOpenDisplay(DisplayInfo *dinfo, char *name)
 
25
{
 
26
        Display *disp;
 
27
        int scrn;
 
28
        if (!(disp = XOpenDisplay(globals.dname)))
 
29
                return FALSE;   /* failed */
 
30
        scrn = DefaultScreen(disp);
 
31
        dinfo->disp = disp;
 
32
        dinfo->scrn = scrn;
 
33
        dinfo->colormap = DefaultColormap(disp, scrn);
 
34
        dinfo->width = DisplayWidth(disp, scrn);
 
35
        dinfo->height = DisplayHeight(disp, scrn);
 
36
        XSetErrorHandler(errorHandler);
 
37
 
 
38
        return TRUE;
 
39
}
 
40
 
 
41
/* report display name when connection fails */
 
42
char *xliDisplayName(char *name)
 
43
{
 
44
        return XDisplayName(name);
 
45
}
 
46
 
 
47
void xliCloseDisplay(DisplayInfo *dinfo)
 
48
{
 
49
        XCloseDisplay(dinfo->disp);
 
50
}
 
51
 
 
52
/*
 
53
simple X11 error handler.  this provides us with some kind of error recovery.
 
54
*/
 
55
 
 
56
int errorHandler(Display * disp, XErrorEvent * error)
 
57
{
 
58
        char errortext[BUFSIZ];
 
59
 
 
60
        XGetErrorText(disp, error->error_code, errortext, BUFSIZ);
 
61
        fprintf(stderr, "xli: X Error: %s on 0x%lx\n",
 
62
                errortext, error->resourceid);
 
63
        if (globals._Xdebug)    
 
64
                abort();
 
65
        else
 
66
                return (0);
 
67
}
 
68
 
 
69
/* Return the default visual class */
 
70
int xliDefaultVisual(void)
 
71
{
 
72
        return (DefaultVisual(globals.dinfo.disp, globals.dinfo.scrn)->class);
 
73
}
 
74
 
 
75
/* return the default depth of the default visual */
 
76
int xliDefaultDepth(void)
 
77
{
 
78
        return DefaultDepth(globals.dinfo.disp, globals.dinfo.scrn);
 
79
}
 
80
 
 
81
/* Print some information about the display */
 
82
void tellAboutDisplay(DisplayInfo * dinfo)
 
83
{
 
84
        Screen *screen;
 
85
        int a, b;
 
86
        if (dinfo->disp) {
 
87
                screen = ScreenOfDisplay(dinfo->disp, dinfo->scrn);
 
88
                printf("Server: %s Version %d\n", ServerVendor(dinfo->disp), VendorRelease(dinfo->disp));
 
89
                printf("Depths and visuals supported:\n");
 
90
                for (a = 0; a < screen->ndepths; a++) {
 
91
                        printf("%2d:", screen->depths[a].depth);
 
92
                        for (b = 0; b < screen->depths[a].nvisuals; b++)
 
93
                                printf(" %s", nameOfVisualClass(screen->depths[a].visuals[b].class));
 
94
                        printf("\n");
 
95
                }
 
96
        } else {
 
97
                printf("[No information on server; error occurred before connection]\n");
 
98
        }
 
99
}
 
100
 
 
101
/* A standalone color name to RGB lookup routine */
 
102
 
 
103
typedef struct {
 
104
        char *name;
 
105
        unsigned char red;
 
106
        unsigned char green;
 
107
        unsigned char blue;
 
108
} xliXColorTableEntry;
 
109
 
 
110
#include "rgbtab.h"             /* Equivalent of an X color table */
 
111
 
 
112
/* parse an X like color. */
 
113
int xliParseXColor(DisplayInfo *dinfo, char *spec, XColor *xcolor)
 
114
{
 
115
        int stat;
 
116
 
 
117
        if (dinfo->disp) {
 
118
                stat = XParseColor(globals.dinfo.disp, globals.dinfo.colormap,
 
119
                                   spec, xcolor);
 
120
        } else {
 
121
                stat = saParseXColor(dinfo, spec, xcolor);
 
122
        }
 
123
 
 
124
        return stat;
 
125
}
 
126
 
 
127
static int saParseXColor(DisplayInfo *dinfo, char *spec, XColor *xcolor)
 
128
{
 
129
        int i, n, r, g, b;
 
130
        xliXColorTableEntry *tp;
 
131
 
 
132
        xcolor->red = 0;
 
133
        xcolor->green = 0;
 
134
        xcolor->blue = 0;
 
135
        n = strlen(spec);
 
136
        if (n > 0) {
 
137
                for (i = 0; i < n; i++) {
 
138
                        if (isupper(spec[i]))
 
139
                                spec[i] = tolower(spec[i]);
 
140
                }
 
141
                if (spec[0] == '#') {
 
142
                        switch (n - 1) {
 
143
                        case 3:
 
144
                                r = hstoi(spec + 1, 1);
 
145
                                g = hstoi(spec + 2, 1);
 
146
                                b = hstoi(spec + 3, 1);
 
147
                                if (r >= 0 && g >= 0 && b >= 0) {
 
148
                                        xcolor->red = r * 0x1111;
 
149
                                        xcolor->green = g * 0x1111;
 
150
                                        xcolor->blue = b * 0x1111;
 
151
 
 
152
                                        return 1;
 
153
                                }
 
154
                                break;
 
155
                        case 6:
 
156
                                r = hstoi(spec + 1, 2);
 
157
                                g = hstoi(spec + 3, 2);
 
158
                                b = hstoi(spec + 5, 2);
 
159
                                if (r >= 0 && g >= 0 && b >= 0) {
 
160
                                        xcolor->red = r * 0x101;
 
161
                                        xcolor->green = g * 0x101;
 
162
                                        xcolor->blue = b * 0x101;
 
163
 
 
164
                                        return 1;
 
165
                                }
 
166
                                break;
 
167
                        case 9:
 
168
                                r = hstoi(spec + 1, 3);
 
169
                                g = hstoi(spec + 4, 3);
 
170
                                b = hstoi(spec + 7, 3);
 
171
                                if (r >= 0 && g >= 0 && b >= 0) {
 
172
                                        xcolor->red = (r * 65535) / 4095;
 
173
                                        xcolor->green = (g * 65535) / 4095;
 
174
                                        xcolor->blue = (b * 65535) / 4095;
 
175
 
 
176
                                        return 1;
 
177
                                }
 
178
                                break;
 
179
                        case 12:
 
180
                                r = hstoi(spec + 1, 4);
 
181
                                g = hstoi(spec + 5, 4);
 
182
                                b = hstoi(spec + 9, 4);
 
183
                                if (r >= 0 && g >= 0 && b >= 0) {
 
184
                                        xcolor->red = r;
 
185
                                        xcolor->green = g;
 
186
                                        xcolor->blue = b;
 
187
 
 
188
                                        return 1;
 
189
                                }
 
190
                                break;
 
191
                        }
 
192
                } else {
 
193
                        /*
 
194
                        else its a color name.  Do a linear search for it 
 
195
                        (binary search would be better)
 
196
                        */
 
197
                        for (n = 0, tp = &xliXColorTable[0]; tp->name[0]; tp++) {
 
198
                                if (strcmp(spec, tp->name) == 0) {
 
199
                                        xcolor->red = tp->red * 0x101;
 
200
                                        xcolor->green = tp->green * 0x101;
 
201
                                        xcolor->blue = tp->blue * 0x101;
 
202
 
 
203
                                        return 1;
 
204
                                }
 
205
                        }
 
206
                }
 
207
        }
 
208
 
 
209
        /* default - return black */
 
210
        xcolor->red = 0;
 
211
        xcolor->green = 0;
 
212
        xcolor->blue = 0;
 
213
 
 
214
        return 0;
 
215
}
 
216
 
 
217
 
 
218
/* gamma correct an XColor */
 
219
void xliGammaCorrectXColor(XColor * xlicolor, double gamma)
 
220
{
 
221
        xlicolor->red = (int) (0.5 + 65535.0 * pow((double) xlicolor->red / 65535.0, gamma));
 
222
        xlicolor->green = (int) (0.5 + 65535.0 * pow((double) xlicolor->green / 65535.0, gamma));
 
223
        xlicolor->blue = (int) (0.5 + 65535.0 * pow((double) xlicolor->blue / 65535.0, gamma));
 
224
}