ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/misc.C
(Generate patch)

Comparing rxvt-unicode/src/misc.C (file contents):
Revision 1.4 by pcg, Sat Jan 31 03:27:45 2004 UTC vs.
Revision 1.30 by root, Tue Jan 3 02:43:33 2006 UTC

1/*--------------------------------*-C-*---------------------------------* 1/*--------------------------------*-C-*---------------------------------*
2 * File: misc.c 2 * File: misc.C
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * 4 *
5 * All portions of code are copyright by their respective author/s. 5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 1996 mj olesen <olesen@me.QueensU.CA> Queen's Univ at Kingston 6 * Copyright (c) 1996 mj olesen <olesen@me.QueensU.CA> Queen's Univ at Kingston
7 * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de> 7 * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de>
8 * Copyright (c) 1998-2000 Geoff Wing <gcw@pobox.com> 8 * Copyright (c) 1998-2000 Geoff Wing <gcw@pobox.com>
9 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
9 * 10 *
10 * This program is free software; you can redistribute it and/or modify 11 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by 12 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or 13 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version. 14 * (at your option) any later version.
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *----------------------------------------------------------------------*/ 24 *----------------------------------------------------------------------*/
24 25
25#include "../config.h" /* NECESSARY */ 26#include "../config.h" /* NECESSARY */
26#include "rxvt.h" /* NECESSARY */ 27#include "rxvt.h" /* NECESSARY */
27#include "misc.intpro" /* PROTOS for internal routines */
28 28
29/* EXTPROTO */ 29char *
30rxvt_wcstombs (const wchar_t *str, int len)
31{
32 if (len < 0) len = wcslen (str);
33
34 mbstate mbs;
35 char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1);
36
37 char *dst = r;
38 while (len--)
39 {
40 ssize_t l = wcrtomb (dst, *str++, mbs);
41 if (l < 0)
42 *dst++ = '?';
43 else
44 dst += l;
45 }
46
47 *dst++ = 0;
48
49 return (char *)rxvt_realloc (r, dst - r);
50}
51
52wchar_t *
53rxvt_mbstowcs (const char *str, int len)
54{
55 if (len < 0) len = strlen (str);
56
57 wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t));
58
59 if ((ssize_t)mbstowcs (r, str, len + 1) < 0)
60 *r = 0;
61
62 return r;
63}
64
65char *
66rxvt_wcstoutf8 (const wchar_t *str, int len)
67{
68 if (len < 0) len = wcslen (str);
69
70 char *r = (char *)rxvt_malloc (len * 4 + 1);
71 char *p = r;
72
73 while (len--)
74 {
75 unicode_t w = *str++ & UNICODE_MASK;
76
77 if (w < 0x000080)
78 *p++ = w;
79 else if (w < 0x000800)
80 *p++ = 0xc0 | ( w >> 6),
81 *p++ = 0x80 | ( w & 0x3f);
82 else if (w < 0x010000)
83 *p++ = 0xe0 | ( w >> 12 ),
84 *p++ = 0x80 | ((w >> 6) & 0x3f),
85 *p++ = 0x80 | ( w & 0x3f);
86 else if (w < 0x110000)
87 *p++ = 0xf0 | ( w >> 18),
88 *p++ = 0x80 | ((w >> 12) & 0x3f),
89 *p++ = 0x80 | ((w >> 6) & 0x3f),
90 *p++ = 0x80 | ( w & 0x3f);
91 else
92 *p++ = '?';
93 }
94
95 *p++ = 0;
96
97 return (char *)rxvt_realloc (r, p - r);
98}
99
100wchar_t *
101rxvt_utf8towcs (const char *str, int len)
102{
103 if (len < 0) len = strlen (str);
104
105 wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)),
106 *p = r;
107
108 unsigned char *s = (unsigned char *)str,
109 *e = s + len;
110
111 for (;;)
112 {
113 len = e - s;
114
115 if (len == 0)
116 break;
117 else if (s[0] < 0x80)
118 *p++ = *s++;
119 else if (len >= 2
120 && s[0] >= 0xc2 && s[0] <= 0xdf
121 && (s[1] & 0xc0) == 0x80)
122 {
123 *p++ = ((s[0] & 0x1f) << 6)
124 | (s[1] & 0x3f);
125 s += 2;
126 }
127 else if (len >= 3
128 && ( (s[0] == 0xe0 && s[1] >= 0xa0 && s[1] <= 0xbf)
129 || (s[0] >= 0xe1 && s[0] <= 0xec && s[1] >= 0x80 && s[1] <= 0xbf)
130 || (s[0] == 0xed && s[1] >= 0x80 && s[1] <= 0x9f)
131 || (s[0] >= 0xee && s[0] <= 0xef && s[1] >= 0x80 && s[1] <= 0xbf)
132 )
133 && (s[2] & 0xc0) == 0x80)
134 {
135 *p++ = ((s[0] & 0x0f) << 12)
136 | ((s[1] & 0x3f) << 6)
137 | (s[2] & 0x3f);
138 s += 3;
139 }
140 else if (len >= 4
141 && ( (s[0] == 0xf0 && s[1] >= 0x90 && s[1] <= 0xbf)
142 || (s[0] >= 0xf1 && s[0] <= 0xf3 && s[1] >= 0x80 && s[1] <= 0xbf)
143 || (s[0] == 0xf4 && s[1] >= 0x80 && s[1] <= 0x8f)
144 )
145 && (s[2] & 0xc0) == 0x80
146 && (s[3] & 0xc0) == 0x80)
147 {
148 *p++ = ((s[0] & 0x07) << 18)
149 | ((s[1] & 0x3f) << 12)
150 | ((s[2] & 0x3f) << 6)
151 | (s[3] & 0x3f);
152 s += 4;
153 }
154 else
155 {
156 *p++ = 0xfffd;
157 s++;
158 }
159 }
160
161 *p = 0;
162
163 return r;
164}
165
30char * 166char *
31rxvt_strdup (const char *str) 167rxvt_strdup (const char *str)
32{ 168{
33 return str ? strdup (str) : 0; 169 return str ? strdup (str) : 0;
34} 170}
35 171
36/* EXTPROTO */ 172char *
37char *
38rxvt_r_basename(const char *str) 173rxvt_r_basename (const char *str)
39{ 174{
40 char *base = STRRCHR(str, '/'); 175 char *base = strrchr (str, '/');
41 176
42 return (char *)(base ? base + 1 : str); 177 return (char *) (base ? base + 1 : str);
43} 178}
44 179
45/* 180/*
46 * Print an error message 181 * Print an error message
47 */ 182 */
48/* EXTPROTO */
49void 183void
184rxvt_vlog (const char *fmt, va_list arg_ptr)
185{
186 char msg[1024];
187
188 vsnprintf (msg, sizeof msg, fmt, arg_ptr);
189
190 if (GET_R && GET_R->log_hook)
191 (*GET_R->log_hook) (msg);
192 else
193 write (STDOUT_FILENO, msg, strlen (msg));
194}
195
196void
50rxvt_print_error(const char *fmt,...) 197rxvt_log (const char *fmt,...)
51{ 198{
52 va_list arg_ptr; 199 va_list arg_ptr;
53 200
54 va_start(arg_ptr, fmt); 201 va_start (arg_ptr, fmt);
55 fprintf(stderr, APL_NAME ": "); 202 rxvt_vlog (fmt, arg_ptr);
56 vfprintf(stderr, fmt, arg_ptr);
57 fprintf(stderr, "\n");
58 va_end(arg_ptr); 203 va_end (arg_ptr);
204}
205
206/*
207 * Print an error message
208 */
209void
210rxvt_warn (const char *fmt,...)
211{
212 va_list arg_ptr;
213
214 rxvt_log ("%s: ", RESNAME);
215
216 va_start (arg_ptr, fmt);
217 rxvt_vlog (fmt, arg_ptr);
218 va_end (arg_ptr);
219}
220
221void
222rxvt_fatal (const char *fmt,...)
223{
224 va_list arg_ptr;
225
226 rxvt_log ("%s: ", RESNAME);
227
228 va_start (arg_ptr, fmt);
229 rxvt_vlog (fmt, arg_ptr);
230 va_end (arg_ptr);
231
232 rxvt_exit_failure ();
233}
234
235class rxvt_failure_exception rxvt_failure_exception;
236
237void
238rxvt_exit_failure ()
239{
240 throw (rxvt_failure_exception);
59} 241}
60 242
61/* 243/*
62 * check that the first characters of S1 match S2 244 * check that the first characters of S1 match S2
63 * 245 *
64 * No Match 246 * No Match
65 * return: 0 247 * return: 0
66 * Match 248 * Match
67 * return: STRLEN (S2) 249 * return: strlen (S2)
68 */ 250 */
69/* EXTPROTO */
70int 251int
71rxvt_Str_match(const char *s1, const char *s2) 252rxvt_Str_match (const char *s1, const char *s2)
72{ 253{
73 int n = STRLEN(s2); 254 int n = strlen (s2);
74 255
75 return ((STRNCMP(s1, s2, n) == 0) ? n : 0); 256 return ((strncmp (s1, s2, n) == 0) ? n : 0);
76} 257}
77 258
78/* EXTPROTO */
79const char * 259const char *
80rxvt_Str_skip_space(const char *str) 260rxvt_Str_skip_space (const char *str)
81{ 261{
82 if (str) 262 if (str)
83 while (*str && isspace(*str)) 263 while (*str && isspace (*str))
84 str++; 264 str++;
265
85 return str; 266 return str;
86} 267}
87 268
88/* 269/*
89 * remove leading/trailing space and strip-off leading/trailing quotes. 270 * remove leading/trailing space and strip-off leading/trailing quotes.
90 * in place. 271 * in place.
91 */ 272 */
92/* EXTPROTO */
93char * 273char *
94rxvt_Str_trim(char *str) 274rxvt_Str_trim (char *str)
95{ 275{
96 char *r, *s; 276 char *r, *s;
97 int n;
98 277
99 if (!str || !*str) /* shortcut */ 278 if (!str || !*str) /* shortcut */
100 return str;
101
102/* skip leading spaces */
103 for (s = str; *s && isspace(*s); s++) ;
104/* goto end of string */
105 for (n = 0, r = s; *r++; n++) ;
106 r -= 2;
107/* dump return */
108 if (n > 0 && *r == '\n')
109 n--, r--;
110/* backtrack along trailing spaces */
111 for (; n > 0 && isspace(*r); r--, n--) ;
112/* skip matching leading/trailing quotes */
113 if (*s == '"' && *r == '"' && n > 1) {
114 s++;
115 n -= 2;
116 }
117/* copy back over: forwards copy */
118 for (r = str; n; n--)
119 *r++ = *s++;
120 *r = '\0';
121
122 return str; 279 return str;
280
281 /* skip leading spaces */
282 for (s = str; *s && isspace (*s); s++) ;
283
284 /* goto end of string */
285 r = s + strlen (s) - 1;
286
287 /* dump return and other trailing whitespace */
288 while (r > s && isspace (*r))
289 r--;
290
291#if 0
292 /* skip matching leading/trailing quotes */
293 if (*s == '"' && *r == '"' && n > 1)
294 {
295 s++;
296 n -= 2;
297 }
298#endif
299
300 memmove (str, s, r + 1 - s);
301 str[r + 1 - s] = 0;
302
303 return str;
123} 304}
124 305
125/* 306/*
126 * in-place interpretation of string: 307 * in-place interpretation of string:
127 * 308 *
134 * "M-x" prefixed strings, append "\r" if needed 315 * "M-x" prefixed strings, append "\r" if needed
135 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed 316 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed
136 * 317 *
137 * returns the converted string length 318 * returns the converted string length
138 */ 319 */
139/* EXTPROTO */
140int 320int
141rxvt_Str_escaped(char *str) 321rxvt_Str_escaped (char *str)
142{ 322{
143 char ch, *s, *d; 323 char ch, *s, *d;
144 int i, num, append = 0; 324 int i, num, append = 0;
145 325
146 if (!str || !*str) 326 if (!str || !*str)
147 return 0; 327 return 0;
148 328
149 d = s = str; 329 d = s = str;
150 330
151 if (*s == 'M' && s[1] == '-') { 331 if (*s == 'M' && s[1] == '-')
332 {
152 /* Emacs convenience, replace leading `M-..' with `\E..' */ 333 /* Emacs convenience, replace leading `M-..' with `\E..' */
153 *d++ = C0_ESC; 334 *d++ = C0_ESC;
154 s += 2; 335 s += 2;
155 if (toupper(*s) == 'X') 336 if (toupper (*s) == 'X')
156 /* append carriage-return for `M-xcommand' */ 337 /* append carriage-return for `M-xcommand' */
157 for (*d++ = 'x', append = '\r', s++; isspace(*s); s++) ; 338 for (*d++ = 'x', append = '\r', s++; isspace (*s); s++) ;
158 } 339 }
159 for (; (ch = *s++);) { 340 for (; (ch = *s++);)
341 {
160 if (ch == '\\') { 342 if (ch == '\\')
343 {
161 ch = *s++; 344 ch = *s++;
162 if (ch >= '0' && ch <= '7') { /* octal */ 345 if (ch >= '0' && ch <= '7')
163 num = ch - '0'; 346 { /* octal */
347 num = ch - '0';
164 for (i = 0; i < 2; i++, s++) { 348 for (i = 0; i < 2; i++, s++)
165 ch = *s; 349 {
350 ch = *s;
166 if (ch < '0' || ch > '7') 351 if (ch < '0' || ch > '7')
167 break; 352 break;
168 num = num * 8 + ch - '0'; 353 num = num * 8 + ch - '0';
169 } 354 }
170 ch = (char)num; 355 ch = (char)num;
171 } else if (ch == 'a') 356 }
357 else if (ch == 'a')
172 ch = C0_BEL; /* bell */ 358 ch = C0_BEL; /* bell */
173 else if (ch == 'b') 359 else if (ch == 'b')
174 ch = C0_BS; /* backspace */ 360 ch = C0_BS; /* backspace */
175 else if (ch == 'E' || ch == 'e') 361 else if (ch == 'E' || ch == 'e')
176 ch = C0_ESC; /* escape */ 362 ch = C0_ESC; /* escape */
363 else if (ch == 'n')
364 ch = '\n'; /* newline */
365 else if (ch == 'r')
366 ch = '\r'; /* carriage-return */
367 else if (ch == 't')
368 ch = C0_HT; /* tab */
369 }
177 else if (ch == 'n') 370 else if (ch == '^')
178 ch = '\n'; /* newline */ 371 {
179 else if (ch == 'r')
180 ch = '\r'; /* carriage-return */
181 else if (ch == 't')
182 ch = C0_HT; /* tab */
183 } else if (ch == '^') {
184 ch = *s++; 372 ch = *s++;
185 ch = toupper(ch); 373 ch = toupper (ch);
186 ch = (ch == '?' ? 127 : (ch - '@')); 374 ch = (ch == '?' ? 127 : (ch - '@'));
187 } 375 }
188 *d++ = ch; 376 *d++ = ch;
189 } 377 }
190 378
191/* ESC] is an XTerm escape sequence, must be terminated */ 379 /* ESC] is an XTerm escape sequence, must be terminated */
192 if (*str == '\0' && str[1] == C0_ESC && str[2] == ']') 380 if (*str == '\0' && str[1] == C0_ESC && str[2] == ']')
193 append = CHAR_ST; 381 append = CHAR_ST;
194 382
195/* add trailing character as required */ 383 /* add trailing character as required */
196 if (append && d[-1] != append) 384 if (append && d[-1] != append)
197 *d++ = append; 385 *d++ = append;
198 *d = '\0'; 386 *d = '\0';
199 387
200 return (d - str); 388 return (d - str);
201} 389}
202 390
203/* 391/*
204 * Split a comma-separated string into an array, stripping leading and 392 * Split a comma-separated string into an array, stripping leading and
205 * trailing spaces (and paired quotes) from each entry. Empty strings 393 * trailing spaces from each entry. Empty strings are properly returned
206 * are properly returned
207 * Caller should free each entry and array when done 394 * Caller should free each entry and array when done
208 */ 395 */
209/* EXTPROTO */
210char ** 396char **
211rxvt_splitcommastring(const char *cs) 397rxvt_splitcommastring (const char *cs)
212{ 398{
213 int l, n, p; 399 int l, n, p;
214 const char *s, *t; 400 const char *s, *t;
215 char **ret; 401 char **ret;
216 402
217 if ((s = cs) == NULL) 403 if ((s = cs) == NULL)
218 s = ""; 404 s = "";
219 405
220 for (n = 1, t = s; *t; t++) 406 for (n = 1, t = s; *t; t++)
221 if (*t == ',') 407 if (*t == ',')
222 n++; 408 n++;
409
223 ret = (char **)malloc((n + 1) * sizeof(char *)); 410 ret = (char **)malloc ((n + 1) * sizeof (char *));
224 ret[n] = NULL; 411 ret[n] = NULL;
225 412
226 for (l = 0, t = s; l < n; l++) { 413 for (l = 0, t = s; l < n; l++)
414 {
227 for ( ; *t && *t != ','; t++) ; 415 for ( ; *t && *t != ','; t++) ;
228 p = t - s; 416 p = t - s;
229 ret[l] = (char *)malloc(p + 1); 417 ret[l] = (char *)malloc (p + 1);
230 strncpy(ret[l], s, p); 418 strncpy (ret[l], s, p);
231 ret[l][p] = '\0'; 419 ret[l][p] = '\0';
232 rxvt_Str_trim(ret[l]); 420 rxvt_Str_trim (ret[l]);
233 s = ++t; 421 s = ++t;
234 } 422 }
423
235 return ret; 424 return ret;
425}
426
427void
428rxvt_freecommastring (char **cs)
429{
430 for (int i = 0; cs[i]; ++i)
431 free (cs[i]);
432
433 free (cs);
236} 434}
237 435
238/*----------------------------------------------------------------------* 436/*----------------------------------------------------------------------*
239 * file searching 437 * file searching
240 */ 438 */
246 * search for FILE in the current working directory, and within the 444 * search for FILE in the current working directory, and within the
247 * colon-delimited PATHLIST, adding the file extension EXT if required. 445 * colon-delimited PATHLIST, adding the file extension EXT if required.
248 * 446 *
249 * FILE is either semi-colon or zero terminated 447 * FILE is either semi-colon or zero terminated
250 */ 448 */
251/* INTPROTO */
252char * 449char *
253rxvt_File_search_path(const char *pathlist, const char *file, const char *ext) 450rxvt_File_search_path (const char *pathlist, const char *file, const char *ext)
254{ 451{
255 int maxpath, len; 452 int maxpath, len;
256 const char *p, *path; 453 const char *p, *path;
257 char name[256]; 454 char name[256];
258 455
259 if (!access(file, R_OK)) /* found (plain name) in current directory */ 456 if (!access (file, R_OK)) /* found (plain name) in current directory */
260 return STRDUP(file); 457 return strdup (file);
261 458
262/* semi-colon delimited */ 459 /* semi-colon delimited */
263 if ((p = STRCHR(file, ';'))) 460 if ((p = strchr (file, ';')))
264 len = (p - file); 461 len = (p - file);
265 else 462 else
266 len = STRLEN(file); 463 len = strlen (file);
267 464
268#ifdef DEBUG_SEARCH_PATH 465#ifdef DEBUG_SEARCH_PATH
269 getcwd(name, sizeof(name)); 466 getcwd (name, sizeof (name));
270 fprintf(stderr, "pwd: \"%s\"\n", name); 467 fprintf (stderr, "pwd: \"%s\"\n", name);
271 fprintf(stderr, "find: \"%.*s\"\n", len, file); 468 fprintf (stderr, "find: \"%.*s\"\n", len, file);
272#endif 469#endif
273 470
274/* leave room for an extra '/' and trailing '\0' */ 471 /* leave room for an extra '/' and trailing '\0' */
275 maxpath = sizeof(name) - (len + (ext ? STRLEN(ext) : 0) + 2); 472 maxpath = sizeof (name) - (len + (ext ? strlen (ext) : 0) + 2);
276 if (maxpath <= 0) 473 if (maxpath <= 0)
277 return NULL;
278
279/* check if we can find it now */
280 STRNCPY(name, file, len);
281 name[len] = '\0';
282
283 if (!access(name, R_OK))
284 return STRDUP(name);
285 if (ext) {
286 STRCAT(name, ext);
287 if (!access(name, R_OK))
288 return STRDUP(name);
289 }
290 for (path = pathlist; path != NULL && *path != '\0'; path = p) {
291 int n;
292
293 /* colon delimited */
294 if ((p = STRCHR(path, ':')) == NULL)
295 p = STRCHR(path, '\0');
296
297 n = (p - path);
298 if (*p != '\0')
299 p++;
300
301 if (n > 0 && n <= maxpath) {
302 STRNCPY(name, path, n);
303 if (name[n - 1] != '/')
304 name[n++] = '/';
305 name[n] = '\0';
306 STRNCAT(name, file, len);
307
308 if (!access(name, R_OK))
309 return STRDUP(name);
310 if (ext) {
311 STRCAT(name, ext);
312 if (!access(name, R_OK))
313 return STRDUP(name);
314 }
315 }
316 }
317 return NULL; 474 return NULL;
318}
319 475
320/* EXTPROTO */ 476 /* check if we can find it now */
477 strncpy (name, file, len);
478 name[len] = '\0';
479
480 if (!access (name, R_OK))
481 return strdup (name);
482 if (ext)
483 {
484 strcat (name, ext);
485 if (!access (name, R_OK))
486 return strdup (name);
487 }
488 for (path = pathlist; path != NULL && *path != '\0'; path = p)
489 {
490 int n;
491
492 /* colon delimited */
493 if ((p = strchr (path, ':')) == NULL)
494 p = strchr (path, '\0');
495
496 n = (p - path);
497 if (*p != '\0')
498 p++;
499
500 if (n > 0 && n <= maxpath)
501 {
502 strncpy (name, path, n);
503 if (name[n - 1] != '/')
504 name[n++] = '/';
505 name[n] = '\0';
506 strncat (name, file, len);
507
508 if (!access (name, R_OK))
509 return strdup (name);
510 if (ext)
511 {
512 strcat (name, ext);
513 if (!access (name, R_OK))
514 return strdup (name);
515 }
516 }
517 }
518 return NULL;
519}
520
321char * 521char *
322rxvt_File_find(const char *file, const char *ext, const char *path) 522rxvt_File_find (const char *file, const char *ext, const char *path)
323{ 523{
324 char *f; 524 char *f;
325 525
326 if (file == NULL || *file == '\0') 526 if (file == NULL || *file == '\0')
327 return NULL; 527 return NULL;
328 528
329/* search environment variables here too */ 529 /* search environment variables here too */
330 if ((f = rxvt_File_search_path(path, file, ext)) == NULL) 530 if ((f = rxvt_File_search_path (path, file, ext)) == NULL)
331#ifdef PATH_ENV 531#ifdef PATH_ENV
332 if ((f = rxvt_File_search_path(getenv(PATH_ENV), file, ext)) == NULL) 532 if ((f = rxvt_File_search_path (getenv (PATH_ENV), file, ext)) == NULL)
333#endif 533#endif
334 f = rxvt_File_search_path(getenv("PATH"), file, ext); 534 f = rxvt_File_search_path (getenv ("PATH"), file, ext);
335 535
336#ifdef DEBUG_SEARCH_PATH 536#ifdef DEBUG_SEARCH_PATH
337 if (f) 537 if (f)
338 fprintf(stderr, "found: \"%s\"\n", f); 538 fprintf (stderr, "found: \"%s\"\n", f);
339#endif 539#endif
340 540
341 return f; 541 return f;
342} 542}
343#endif /* defined (XPM_BACKGROUND) || (MENUBAR_MAX) */ 543#endif /* defined (XPM_BACKGROUND) || (MENUBAR_MAX) */
344 544
345/*----------------------------------------------------------------------* 545/*----------------------------------------------------------------------*
346 * miscellaneous drawing routines 546 * miscellaneous drawing routines
348 548
349/* 549/*
350 * Draw top/left and bottom/right border shadows around windows 550 * Draw top/left and bottom/right border shadows around windows
351 */ 551 */
352#if defined(RXVT_SCROLLBAR) || defined(MENUBAR) 552#if defined(RXVT_SCROLLBAR) || defined(MENUBAR)
353/* EXTPROTO */
354void 553void
355rxvt_Draw_Shadow(Display *Xdisplay, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h) 554rxvt_Draw_Shadow (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h)
356{ 555{
357 int shadow; 556 int shadow;
358 557
359 shadow = (w == 0 || h == 0) ? 1 : SHADOW; 558 shadow = (w == 0 || h == 0) ? 1 : MENU_SHADOW;
360 w += x - 1; 559 w += x - 1;
361 h += y - 1; 560 h += y - 1;
362 for (; shadow-- > 0; x++, y++, w--, h--) { 561 for (; shadow-- > 0; x++, y++, w--, h--)
562 {
363 XDrawLine(Xdisplay, win, topShadow, x, y, w, y); 563 XDrawLine (display, win, topShadow, x, y, w, y);
364 XDrawLine(Xdisplay, win, topShadow, x, y, x, h); 564 XDrawLine (display, win, topShadow, x, y, x, h);
365 XDrawLine(Xdisplay, win, botShadow, w, h, w, y + 1); 565 XDrawLine (display, win, botShadow, w, h, w, y + 1);
366 XDrawLine(Xdisplay, win, botShadow, w, h, x + 1, h); 566 XDrawLine (display, win, botShadow, w, h, x + 1, h);
367 } 567 }
368} 568}
369#endif 569#endif
370 570
371/* button shapes */ 571/* button shapes */
372#ifdef MENUBAR 572#ifdef MENUBAR
373/* EXTPROTO */
374void 573void
375rxvt_Draw_Triangle(Display *Xdisplay, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type) 574rxvt_Draw_Triangle (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type)
376{ 575{
377 switch (type) { 576 switch (type)
577 {
378 case 'r': /* right triangle */ 578 case 'r': /* right triangle */
379 XDrawLine(Xdisplay, win, topShadow, x, y, x, y + w); 579 XDrawLine (display, win, topShadow, x, y, x, y + w);
380 XDrawLine(Xdisplay, win, topShadow, x, y, x + w, y + w / 2); 580 XDrawLine (display, win, topShadow, x, y, x + w, y + w / 2);
381 XDrawLine(Xdisplay, win, botShadow, x, y + w, x + w, y + w / 2); 581 XDrawLine (display, win, botShadow, x, y + w, x + w, y + w / 2);
382 break; 582 break;
383 583
384 case 'l': /* left triangle */ 584 case 'l': /* left triangle */
385 XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x + w, y); 585 XDrawLine (display, win, botShadow, x + w, y + w, x + w, y);
386 XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x, y + w / 2); 586 XDrawLine (display, win, botShadow, x + w, y + w, x, y + w / 2);
387 XDrawLine(Xdisplay, win, topShadow, x, y + w / 2, x + w, y); 587 XDrawLine (display, win, topShadow, x, y + w / 2, x + w, y);
388 break; 588 break;
389 589
390 case 'd': /* down triangle */ 590 case 'd': /* down triangle */
391 XDrawLine(Xdisplay, win, topShadow, x, y, x + w / 2, y + w); 591 XDrawLine (display, win, topShadow, x, y, x + w / 2, y + w);
392 XDrawLine(Xdisplay, win, topShadow, x, y, x + w, y); 592 XDrawLine (display, win, topShadow, x, y, x + w, y);
393 XDrawLine(Xdisplay, win, botShadow, x + w, y, x + w / 2, y + w); 593 XDrawLine (display, win, botShadow, x + w, y, x + w / 2, y + w);
394 break; 594 break;
395 595
396 case 'u': /* up triangle */ 596 case 'u': /* up triangle */
397 XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x + w / 2, y); 597 XDrawLine (display, win, botShadow, x + w, y + w, x + w / 2, y);
398 XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x, y + w); 598 XDrawLine (display, win, botShadow, x + w, y + w, x, y + w);
399 XDrawLine(Xdisplay, win, topShadow, x, y + w, x + w / 2, y); 599 XDrawLine (display, win, topShadow, x, y + w, x + w / 2, y);
400 break; 600 break;
401#if 0 601#if 0
402 case 's': /* square */ 602 case 's': /* square */
403 XDrawLine(Xdisplay, win, topShadow, x + w, y, x, y); 603 XDrawLine (display, win, topShadow, x + w, y, x, y);
404 XDrawLine(Xdisplay, win, topShadow, x, y, x, y + w); 604 XDrawLine (display, win, topShadow, x, y, x, y + w);
405 XDrawLine(Xdisplay, win, botShadow, x, y + w, x + w, y + w); 605 XDrawLine (display, win, botShadow, x, y + w, x + w, y + w);
406 XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x + w, y); 606 XDrawLine (display, win, botShadow, x + w, y + w, x + w, y);
407 break; 607 break;
408#endif 608#endif
609
409 } 610 }
410} 611}
411#endif 612#endif
613
614// should not be used in interactive programs, for obvious reasons
615void rxvt_usleep (int usecs)
616{
617#if HAVE_NANOSLEEP
618 struct timespec ts;
619
620 ts.tv_sec = 0;
621 ts.tv_nsec = usecs * 1000;
622 nanosleep (&ts, NULL);
623#else
624 /* use select for timing */
625 struct timeval tv;
626
627 tv.tv_sec = 0;
628 tv.tv_usec = usecs;
629 select (0, NULL, NULL, NULL, &tv);
630#endif
631}
632
412/*----------------------- end-of-file (C source) -----------------------*/ 633/*----------------------- end-of-file (C source) -----------------------*/
634

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines