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
|
/*
* Matchbox Keyboard - A lightweight software keyboard.
*
* Authored By Matthew Allum <mallum@o-hand.com>
*
* Copyright (c) 2005-2012 Intel Corp
*
* SPDX-License-Identifier: LGPL-2.1
*
*/
#include "matchbox-keyboard.h"
static int TrappedErrorCode = 0;
static int (*old_error_handler) (Display *, XErrorEvent *);
static int
error_handler(Display *xdpy,
XErrorEvent *error)
{
TrappedErrorCode = error->error_code;
return 0;
}
void
util_trap_x_errors(void)
{
TrappedErrorCode = 0;
old_error_handler = XSetErrorHandler(error_handler);
}
int
util_untrap_x_errors(void)
{
XSetErrorHandler(old_error_handler);
return TrappedErrorCode;
}
void*
util_malloc0(int size)
{
void *p;
p = malloc(size);
memset(p, 0, size);
return p;
}
void
util_fatal_error(char *msg)
{
fprintf(stderr, "matchbox-keyboard: *Error* %s", msg);
exit(1);
}
#define UTF8_COMPUTE(Char, Mask, Len) \
if (Char < 128) \
{ \
Len = 1; \
Mask = 0x7f; \
} \
else if ((Char & 0xe0) == 0xc0) \
{ \
Len = 2; \
Mask = 0x1f; \
} \
else if ((Char & 0xf0) == 0xe0) \
{ \
Len = 3; \
Mask = 0x0f; \
} \
else if ((Char & 0xf8) == 0xf0) \
{ \
Len = 4; \
Mask = 0x07; \
} \
else if ((Char & 0xfc) == 0xf8) \
{ \
Len = 5; \
Mask = 0x03; \
} \
else if ((Char & 0xfe) == 0xfc) \
{ \
Len = 6; \
Mask = 0x01; \
} \
else \
Len = -1;
int
util_utf8_char_cnt(const char *str)
{
const unsigned char *p = (unsigned char *)str;
int mask, len, result = 0;
/* XXX Should validate too */
while (*p != '\0')
{
UTF8_COMPUTE(*p, mask, len);
p += len;
result++;
}
return result;
}
boolean
util_file_readable(char *path)
{
struct stat st;
if (stat(path, &st))
return False;
return True;
}
|