File: xutils.c

package info (click to toggle)
xspectemu 0.94-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,176 kB
  • ctags: 3,073
  • sloc: ansic: 15,394; asm: 5,160; sh: 1,528; cpp: 377; makefile: 186
file content (301 lines) | stat: -rw-r--r-- 7,571 bytes parent folder | download | duplicates (13)
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
/* 
 * Copyright (C) 1996-1998 Szeredi Miklos
 * Email: mszeredi@inf.bme.hu
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. See the file COPYING. 
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/* #define DEBUG_COLOR */

#include "xscr.h"
#include "spscr.h"
#include "spscr_p.h"
#include "ax.h"
#include "xdispkb.h"
#include "spperif.h"
#include "interf.h"
#include "spconf_p.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>

#include <X11/Xutil.h>
#include <X11/Xatom.h>

#define MAX_SEARCH_DEPTH 8


int privatemap = 0;

static unsigned cdist(XColor *c1, XColor *c2)
{
  int dr, dg, db;

  dr = (c1->red - c2->red) >> 8;
  dg = (c1->green - c2->green) >> 8;
  db =  (c1->blue - c2->blue) >> 8;

  return dr * dr + dg * dg + db * db;
}

static int already_alloced(pixt *colors, int num, pixt pix)
{
  int i;

  for(i = 0; i < num; i++) if(colors[i] == pix) return 1;
  return 0;
}

#define MYINF ((unsigned) -1)

static int search_colors(Colormap cmap, unsigned dp)
{
  int c;
  XColor xcolor, tmpcolor;
  pixt pix;

  XColor *scrcolors = NULL;
  unsigned  ncolors = 0;
  unsigned dist, mindist;
  pixt minpix;
  int failed = 0, lfailed;
  int fails[COLORNUM];

  if(dp < 4) failed = 1;
  if(!failed) {
    if(dp <= MAX_SEARCH_DEPTH) {
      ncolors = 1 << dp;
      scrcolors = malloc(sizeof(XColor) * ncolors);

      if(scrcolors != NULL) {
	for(pix = 0; pix < ncolors; pix++) scrcolors[pix].pixel = pix;
	XQueryColors(xsp_disp, cmap, scrcolors, (int) ncolors);
      }
    }
    
    
    for(c = 0; c < COLORNUM; c++) {
      xcolor.red = spscr_crgb[c].r << 10;
      xcolor.green = spscr_crgb[c].g << 10;
      xcolor.blue = spscr_crgb[c].b << 10;
      lfailed = 0;
      if(!XAllocColor(xsp_disp, cmap, &xcolor) || 
         already_alloced(xsp_colors, c, xcolor.pixel)) {
        
        if(scrcolors != NULL) {
#ifdef DEBUG_COLOR
          fprintf(stderr, "XAllocColor failed, searching all colors...\n");
#endif
          mindist = MYINF;
          
          for(pix = 0; pix < ncolors; pix++) {
            dist = cdist(&scrcolors[pix], &xcolor);
            if(dist < mindist) {
              tmpcolor.red = scrcolors[pix].red;
              tmpcolor.green = scrcolors[pix].green;
              tmpcolor.blue = scrcolors[pix].blue;
              if(XAllocColor(xsp_disp, cmap, &tmpcolor) &&
                 !already_alloced(xsp_colors, c, tmpcolor.pixel)) {
                if(mindist != MYINF) 
                  XFreeColors(xsp_disp, cmap, &minpix, 1, 0);
                mindist = dist;
                minpix = pix;
              }
            }
          }
          
          if(mindist == MYINF) {
            lfailed = 1;
#ifdef DEBUG_COLOR
            fprintf(stderr, "Search failed\n");
#endif
          }
          else xsp_colors[c] = minpix;
        }
        else {
#ifdef DEBUG_COLOR  
          fprintf(stderr, "XAllocColor failed\n");
#endif
          lfailed = 1;
        }
      }
      else xsp_colors[c] = xcolor.pixel;
      
      if(lfailed) {
	fails[c] = 1;
	failed = 1;
      }
      else fails[c] = 0;
      
      
#ifdef DEBUG_COLOR
      tmpcolor.pixel = xsp_colors[c];
      XQueryColor(xsp_disp, cmap, &tmpcolor);
      
      
      fprintf(stderr, 
              "Color %2i (%3i %3i %3i) -> Pixel %5lu (%3i %3i %3i)\n",
              c, spscr_crgb[c].r, spscr_crgb[c].g, spscr_crgb[c].b,
              xsp_colors[c], tmpcolor.red>>10, tmpcolor.green>>10, 
              tmpcolor.blue>>10);
#endif    
      
    }
    if(failed) {
      for(c = 0; c < COLORNUM; c++) {
	if(!fails[c]) XFreeColors(xsp_disp, cmap, &xsp_colors[c], 1, 0);
	xsp_colors[c] = c;
      }
    }
  }
  if(scrcolors != NULL) free(scrcolors);

  if(failed) return 0;
  return 1;
}

static int firstalloced = 1;
static int currprivate;

int allocate_colors(void)
{
  int color_res;
  Colormap defcmap;

  if(!firstalloced && !currprivate) 
    XFreeColors(xsp_disp, xsp_cmap, xsp_colors, COLORNUM, 0);
  
  defcmap = DefaultColormap(xsp_disp, xsp_scr);
  color_res = search_colors(defcmap, xsp_depth);

  if((!color_res || privatemap) && 
     (xsp_visual->class == PseudoColor || xsp_visual->class == GrayScale)) {
    XColor xcolor;
    int c;
    
    if(color_res) XFreeColors(xsp_disp, defcmap, xsp_colors, COLORNUM, 0);

    if(firstalloced || !currprivate) 
      xsp_cmap = XCreateColormap(xsp_disp, xsp_win, xsp_visual, AllocAll);

    if(xsp_depth <= MAX_SEARCH_DEPTH) {   /* Is this good? */
      for(c = 0; c < (1 << xsp_depth); c++) {
	xcolor.pixel = c;
	XQueryColor(xsp_disp, defcmap, &xcolor);
	XStoreColor(xsp_disp, xsp_cmap, &xcolor);
      }
    }

    for(c = 0; c < COLORNUM; c++) {
      xcolor.pixel = xsp_colors[c];
      xcolor.red = spscr_crgb[c].r << 10;
      xcolor.green = spscr_crgb[c].g << 10;
      xcolor.blue = spscr_crgb[c].b << 10;
      xcolor.flags = DoRed | DoGreen | DoBlue;
      XStoreColor(xsp_disp, xsp_cmap, &xcolor);
    }
    color_res = 1;
    currprivate = 1;
  }
  else {
    if(!firstalloced && currprivate) XFreeColormap(xsp_disp, xsp_cmap);
    xsp_cmap = defcmap;
    currprivate = 0;
  }
  firstalloced = 0;

  return color_res;
}


void spscr_refresh_colors(void)
{
  if(!allocate_colors()) put_msg("Could not allocate colors");
  
  kb_refresh_colormap();
  XSetWindowColormap(xsp_disp, xsp_win, xsp_cmap);
  XSetWindowBackground(xsp_disp, xsp_win, xsp_colors[7]);
  XInstallColormap(xsp_disp, xsp_cmap);
  spxs_init_color();
  sp_init_screen_mark();
}


#define MAX_RES_NAME 64
#define MAXLINELEN 512

void spcf_read_xresources(void)
{
  int i;
  char resname[MAX_RES_NAME], resclass[MAX_RES_NAME];
  char line[MAXLINELEN+1];
  char *val;

  resname[0] = '.';
  resclass[0] = '.';
  resname[MAX_RES_NAME-1] = '\0';
  resclass[MAX_RES_NAME-1] = '\0';
  
  for(i = 0; spcf_options[i].option != NULL; i++) {
    strncpy(resname+1, spcf_options[i].option, MAX_RES_NAME-2);
    strncpy(resclass+1, spcf_options[i].option, MAX_RES_NAME-2);
    resclass[1] = toupper(resclass[1]);

    val = aX_get_prog_res(resname, resclass);
    if(val != NULL) spcf_set_val(i, val, NULL, 0, 0);
  }
  
  strcpy(resname+1, "color");
  strcpy(resclass+1, "Color");

  for(i = 0; i < COLORNUM; i++) {
    sprintf(resname+6, "%i", i);
    sprintf(resclass+6, "%i", i);

    val = aX_get_prog_res(resname, resclass);
    if(val != NULL) spcf_set_color(i, val, NULL, 0, 0);
  }

  strcpy(resname+1, "keys");
  strcpy(resclass+1, "Keys");
  
  val = aX_get_prog_res(resname, resclass);
  if(val != NULL) {
    char *ival, *attr;
    int l;

    while(*val) {
      for(i = 0; val[i] && val[i] != ';'; i++);
      l = i;
      if(i > MAXLINELEN) i = MAXLINELEN;
      strncpy(line, val, (size_t) i);
      line[i] = '\0';
      
      if(spcf_parse_conf_line(line, &attr, &ival)) {
	int ix;
	
	ix = spcf_match_keydef(attr, "");
	if(ix >= 0) spcf_set_key(ix, ival, NULL, 0, 0);
      }
      
      if(!val[l]) break;
      val += (l+1);
    }
  }
}