1 #include "ecore_xcb_private.h"
2 //#include "Ecore_X_Atoms.h"
8 # define CODESET "INVALID"
11 static int _ecore_xcb_textlist_get_buffer_size(Eina_Bool is_wide,
14 static int _ecore_xcb_textlist_get_wc_len(wchar_t *wstr);
15 static void *_ecore_xcb_textlist_alloc_list(Eina_Bool is_wide,
18 static void _ecore_xcb_textlist_copy_list(Eina_Bool is_wide,
22 static wchar_t *_ecore_xcb_textlist_copy_wchar(wchar_t *str1,
24 static int _ecore_xcb_textlist_len_wchar(wchar_t *str);
28 _ecore_xcb_utf8_textlist_to_textproperty(char **list,
30 Ecore_Xcb_Encoding_Style style,
31 Ecore_Xcb_Textproperty *ret)
33 LOGFN(__FILE__, __LINE__, __FUNCTION__);
35 return _ecore_xcb_textlist_to_textproperty("utf8string", list, count,
42 _ecore_xcb_mb_textlist_to_textproperty(char **list,
44 Ecore_Xcb_Encoding_Style style,
45 Ecore_Xcb_Textproperty *ret)
47 LOGFN(__FILE__, __LINE__, __FUNCTION__);
49 return _ecore_xcb_textlist_to_textproperty("multiByte", list, count,
53 /* NB: This Function May Not Be Correct !!!
54 * (as I do not know text conversion, locales, etc, etc very well)
56 * Portions were ripped from libX11 XTextListToTextProperty
59 _ecore_xcb_textlist_to_textproperty(const char *type,
62 Ecore_Xcb_Encoding_Style style,
63 Ecore_Xcb_Textproperty *ret)
65 Eina_Bool is_wide = EINA_FALSE;
66 Ecore_X_Atom encoding;
67 int len = 0, nitems = 0, i = 0;
68 size_t from_left = 0, to_left = 0;
69 int unconv_num = 0, val = 0;
70 char *buff, *to, *value, *from;
71 const char *to_type, *from_type;
78 LOGFN(__FILE__, __LINE__, __FUNCTION__);
80 if (!strcmp("wideChar", type)) is_wide = EINA_TRUE;
81 len = _ecore_xcb_textlist_get_buffer_size(is_wide, list, count);
82 if (!(buff = (char *)malloc(len * sizeof(char)))) return EINA_FALSE;
83 from_type = nl_langinfo(CODESET);
87 case XcbStdICCTextStyle:
88 encoding = ECORE_X_ATOM_STRING;
89 to_type = nl_langinfo(CODESET);
90 // to_type = "string";
93 case XcbUTF8StringStyle:
94 encoding = ECORE_X_ATOM_UTF8_STRING;
98 case XcbCompoundTextStyle:
99 encoding = ECORE_X_ATOM_COMPOUND_TEXT;
100 to_type = nl_langinfo(CODESET);
101 // to_type = "compoundText";
105 encoding = ECORE_X_ATOM_TEXT;
106 to_type = nl_langinfo(CODESET);
107 // to_type = "multiByte";
113 for (i = 0; ((i < count) && (len > 0)); i++)
115 if (*mb) strcpy(to, *mb);
117 from_left = (*mb ? strlen(*mb) : 0) + 1;
141 conv = iconv_open(to_type, from_type);
145 wc = (wchar_t **)list;
152 for (i = 1; to_left > 0; i++)
157 from_left = _ecore_xcb_textlist_get_wc_len(*wc);
163 from_left = (*mb ? strlen(*mb) : 0);
168 val = iconv(conv, &from, &from_left, &to, &to_left);
170 if (val < 0) continue;
171 if ((val > 0) && (style == XcbStdICCTextStyle) &&
172 (encoding == ECORE_X_ATOM_STRING))
177 encoding = ECORE_X_ATOM_COMPOUND_TEXT;
184 if (i >= count) break;
190 nitems = (to - buff);
193 if (nitems <= 0) nitems = 1;
194 if (!(value = (char *)malloc(nitems * sizeof(char))))
202 memcpy(value, buff, nitems);
207 ret->encoding = encoding;
209 ret->nitems = nitems;
216 _ecore_xcb_utf8_textproperty_to_textlist(const Ecore_Xcb_Textproperty *text_prop,
220 LOGFN(__FILE__, __LINE__, __FUNCTION__);
222 return _ecore_xcb_textproperty_to_textlist(text_prop, "utf8String",
223 list_ret, count_ret);
229 _ecore_xcb_mb_textproperty_to_textlist(const Ecore_Xcb_Textproperty *text_prop,
233 LOGFN(__FILE__, __LINE__, __FUNCTION__);
235 return _ecore_xcb_textproperty_to_textlist(text_prop, "multiByte",
236 list_ret, count_ret);
240 _ecore_xcb_textproperty_to_textlist(const Ecore_Xcb_Textproperty *text_prop,
245 Eina_Bool is_wide = EINA_FALSE;
246 Eina_Bool do_strcpy = EINA_FALSE;
247 const char *from_type;
248 char *buff, *to, *from;
250 int nitems = 0, len = 0, num = 0, ret = 0;
251 size_t from_left = 0, to_left = 0;
258 if (!strcmp("wideChar", type)) is_wide = EINA_TRUE;
260 nitems = text_prop->nitems;
261 if (nitems <= 0) return EINA_TRUE;
263 if (text_prop->format != 8) return EINA_FALSE;
265 from_type = nl_langinfo(CODESET);
266 if (text_prop->encoding == ECORE_X_ATOM_UTF8_STRING)
270 len = (text_prop->nitems + 1) * sizeof(wchar_t);
273 if (!strcmp(type, "utf8String"))
274 len = text_prop->nitems * 6 + 1;
276 len = text_prop->nitems * MB_CUR_MAX + 1;
279 buff = (char *)malloc(len * sizeof(char));
280 if (!buff) return EINA_FALSE;
285 if (!strcmp(from_type, type))
286 do_strcpy = EINA_TRUE;
290 conv = iconv_open(type, from_type);
299 lptr = sptr = text_prop->value;
300 num = *count_ret = 0;
303 if ((nitems == 0) || (*sptr == 0))
306 from_left = sptr - lptr;
312 l = MIN(from_left, to_left);
313 strncpy(to, from, l);
321 ret = iconv(conv, &from, &from_left, &to, &to_left);
323 if (ret < 0) continue;
326 if (nitems == 0) break;
330 *((wchar_t *)to) = (wchar_t)0;
331 to += sizeof(wchar_t);
332 to_left -= sizeof(wchar_t);
336 *((char *)to) = '\0';
348 if (!do_strcpy) iconv_close(conv);
353 *((wchar_t *)to) = (wchar_t)0;
354 to_left -= sizeof(wchar_t);
358 *((char *)to) = '\0';
363 _ecore_xcb_textlist_alloc_list(is_wide, *count_ret, (len - to_left));
365 _ecore_xcb_textlist_copy_list(is_wide, buff, *list_ret, *count_ret);
373 _ecore_xcb_textlist_get_buffer_size(Eina_Bool is_wide,
384 wc = (wchar_t **)list;
385 for (; count-- > 0; wc++)
386 if (*wc) len += _ecore_xcb_textlist_get_wc_len(*wc) + 1;
392 for (; count-- > 0; mb++)
393 if (*mb) len += strlen(*mb) + 1;
396 len = (len / 2048 + 1) * 2048;
401 _ecore_xcb_textlist_get_wc_len(wchar_t *wstr)
413 _ecore_xcb_textlist_alloc_list(Eina_Bool is_wide,
421 list = (wchar_t **)malloc(count * sizeof(wchar_t *));
422 if (!list) return NULL;
423 *list = (wchar_t *)malloc(nitems * sizeof(wchar_t));
435 list = (char **)malloc(count * sizeof(char *));
436 if (!list) return NULL;
437 *list = (char *)malloc(nitems * sizeof(char));
448 _ecore_xcb_textlist_copy_list(Eina_Bool is_wide,
457 wchar_t *txt, *str, **wlist;
459 txt = (wchar_t *)text;
460 wlist = (wchar_t **)list;
461 for (str = *wlist; count > 0; count--, wlist++)
463 _ecore_xcb_textlist_copy_wchar(str, txt);
465 len = (_ecore_xcb_textlist_len_wchar(str) + 1);
472 char *txt, *str, **slist;
475 slist = (char **)list;
476 for (str = *slist; count > 0; count--, slist++)
480 len = strlen(str) + 1;
488 _ecore_xcb_textlist_copy_wchar(wchar_t *str1,
494 while ((*str1++ = *str2++))
500 _ecore_xcb_textlist_len_wchar(wchar_t *str)