1 /** @file scim_utility.h
2 * @brief various utility functions.
5 /* ISF is based on SCIM 1.4.7 and extended for supporting more mobile fitable. */
8 * Smart Common Input Method
10 * Copyright (c) 2002-2005 James Su <suzhe@tsinghua.org.cn>
11 * Copyright (c) 2012-2015 Samsung Electronics Co., Ltd.
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this program; if not, write to the
26 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
27 * Boston, MA 02111-1307 USA
29 * Modifications by Samsung Electronics Co., Ltd.
30 * 1. Add time and logs functions for performance profile
31 * 2. Define RECT_INFO, ISF_CANDIDATE_MODE_T and ISF_CANDIDATE_PORTRAIT_LINE_T
33 * $Id: scim_utility.h,v 1.36 2005/04/09 15:38:39 suzhe Exp $
36 #ifndef __SCIM_UTILITY_H
37 #define __SCIM_UTILITY_H
43 * @addtogroup Accessories
44 * @ingroup InputServiceFramework
48 #ifndef SCIM_KEYBOARD_ICON_FILE
49 #define SCIM_KEYBOARD_ICON_FILE (SCIM_ICONDIR "/keyboard.png")
52 #define SCIM_PATH_DELIM_STRING "/"
53 #define SCIM_PATH_DELIM '/'
55 // UTF-8 <-> ucs4_t convert
57 /* Return code if invalid. (xxx_mbtowc, xxx_wctomb) */
59 /* Return code if only a shift sequence of n bytes was read. (xxx_mbtowc) */
60 #define RET_TOOFEW(n) (-1-(n))
61 /* Return code if output buffer is too small. (xxx_wctomb, xxx_reset) */
62 #define RET_TOOSMALL -1
63 /* Replacement character for invalid multibyte sequence or wide character. */
64 #define BAD_WCHAR ((ucs4_t) 0xfffd)
68 * @brief Convert an utf8 char sequence to ucs4.
70 * @param pwc destination buffer to store the ucs4 code.
71 * @param src source buffer contains the utf8 char sequence.
72 * @param src_len the size of source buffer.
74 * @return number of chars in s actually converted.
76 EXAPI int utf8_mbtowc (ucs4_t *pwc, const unsigned char *src, int src_len);
79 * @brief Convert an ucs4 code to utf8 char sequence.
81 * @param dest destination buffer to store utf8 char sequence.
82 * @param wc the ucs4 code to be converted.
83 * @param dest_size the size of destination buffer.
85 * @return the number of bytes actually written into dest.
87 EXAPI int utf8_wctomb (unsigned char *dest, ucs4_t wc, int dest_size);
90 * @brief Convert an utf8 string to an ucs4 string.
92 * @param str source utf8 string.
93 * @return the destination widestring.
95 EXAPI WideString utf8_mbstowcs (const String & str);
98 * @brief Convert an utf8 string to an ucs4 string.
100 * @param str source utf8 string.
101 * @param len length of the source string.
102 * @return the destination widestring.
104 EXAPI WideString utf8_mbstowcs (const char *str, int len = -1);
107 * @brief Convert an ucs4 string to an utf8 string.
109 * @param wstr source ucs4 string.
111 * @return the destination utf8 string.
113 EXAPI String utf8_wcstombs (const WideString & wstr);
116 * @brief Convert an ucs4 string to an utf8 string.
118 * @param wstr source ucs4 string.
119 * @param len length of the source string.
121 * @return the destination utf8 string.
123 EXAPI String utf8_wcstombs (const ucs4_t *wstr, int len = -1);
126 * @brief Read a wide char from istream.
128 * The content in the istream are actually in utf-8 encoding.
130 * @param is the stream to be read.
132 * @return if equal to 0 then got the end of the stream or error occurred.
134 EXAPI ucs4_t utf8_read_wchar (std::istream &is);
137 * @brief Write a wide char to ostream.
139 * The content written into the ostream will be converted into utf-8 encoding.
141 * @param os the stream to be written.
142 * @param wc the wide char to be written to the stream.
143 * @return the same stream object reference.
145 EXAPI std::ostream & utf8_write_wchar (std::ostream &os, ucs4_t wc);
148 * @brief Read a wide string from istream.
150 * The content in the istream are actually in utf-8 encoding.
152 * @param is the stream to be read.
153 * @param delim the delimiter of the string.
154 * @param rm_delim if the delim should be removed from the destination string.
155 * @return the wide string read from the given stream.
157 EXAPI WideString utf8_read_wstring (std::istream &is, ucs4_t delim = (ucs4_t) '\n', bool rm_delim = true);
160 * @brief Write a wide string to ostream.
162 * The content written into the ostream will be converted into utf-8 encoding.
164 * @param os the stream to be written.
165 * @param wstr the wide string to be written into the stream.
166 * @return the same stream object reference.
168 EXAPI std::ostream & utf8_write_wstring (std::ostream &os, const WideString & wstr);
171 * @brief Convert an uint32 variable into a sequence of bytes.
173 * @param bytes the buffer to store the result.
174 * @param n the variable to be converted.
177 void scim_uint32tobytes (unsigned char *bytes, uint32 n)
179 bytes [0] = (unsigned char) ((n & 0xFF));
180 bytes [1] = (unsigned char) ((n >> 8) & 0xFF);
181 bytes [2] = (unsigned char) ((n >> 16) & 0xFF);
182 bytes [3] = (unsigned char) ((n >> 24) & 0xFF);
186 * @brief Convert a sequence of bytes into an uint32 value.
188 * @param bytes the buffer contains the bytes to be converted.
189 * @return the result uint32 value.
192 uint32 scim_bytestouint32 (const unsigned char *bytes)
194 return ((uint32) bytes [0])
195 | (((uint32) bytes [1]) << 8)
196 | (((uint32) bytes [2]) << 16)
197 | (((uint32) bytes [3]) << 24);
201 * @brief Convert an uint16 variable into a sequence of bytes.
203 * @param bytes the buffer to store the result.
204 * @param n the variable to be converted.
207 void scim_uint16tobytes (unsigned char *bytes, uint16 n)
209 bytes [0] = (unsigned char) ((n & 0xFF));
210 bytes [1] = (unsigned char) ((n >> 8) & 0xFF);
214 * @brief Convert a sequence of bytes into an uint16 value.
216 * @param bytes the buffer contains the bytes to be converted.
217 * @return the result uint16 value.
220 uint16 scim_bytestouint16 (const unsigned char *bytes)
222 return ((uint16) bytes [0]) | (((uint16) bytes [1]) << 8);
226 * @brief Test if the locale is valid, and return the good locale name.
228 * @param locale the locale to be tested.
229 * @return If the locale is valid, it's the good locale name, otherwise empty.
231 EXAPI String scim_validate_locale (const String& locale);
234 * @brief Get the encoding for a locale.
236 * @param locale the name of the locale.
237 * @return The encoding used by the given locale.
239 EXAPI String scim_get_locale_encoding (const String& locale);
242 * @brief Get current system locale.
243 * @return The current system locale.
245 EXAPI String scim_get_current_locale ();
248 * @brief Get current system language.
249 * @return The current system language.
251 EXAPI String scim_get_current_language ();
254 * @brief Get the max length of the multibyte char of a locale.
256 * @param locale the name of the locale.
257 * @return the maxlen of this locale.
259 EXAPI int scim_get_locale_maxlen (const String& locale);
262 * @brief Split string list into a string vector according to the delim char.
264 * @param vec the string vector to store the result.
265 * @param str the string to be splitted.
266 * @param delim the delimiter to split the strings.
267 * @return the number of the strings in the result list.
269 EXAPI int scim_split_string_list (std::vector<String>& vec, const String& str, char delim = ',');
272 * @brief Combine a string vector into one string list, separated by char delim.
274 * @param vec the string vector which contains the strings to be combined.
275 * @param delim the delimiter which should be put between two strings.
276 * @return the result string.
278 EXAPI String scim_combine_string_list (const std::vector<String>& vec, char delim = ',');
281 * @brief Get machine endian type
282 * @return 1 little endian, 0 big endian
284 EXAPI bool scim_is_little_endian ();
287 * @brief Test if wchar_t is using UCS4 encoding.
289 EXAPI bool scim_if_wchar_ucs4_equal ();
292 * @brief Convert a half width unicode char to its full width counterpart.
294 EXAPI ucs4_t scim_wchar_to_full_width (ucs4_t code);
297 * @brief Convert a full width unicode char to its half width counterpart.
299 EXAPI ucs4_t scim_wchar_to_half_width (ucs4_t code);
302 * @brief Get the home dir of current user.
304 EXAPI String scim_get_home_dir ();
307 * @brief Get the name of current user.
309 EXAPI String scim_get_user_name ();
312 * @brief Get SCIM data dir of current user.
314 EXAPI String scim_get_user_data_dir ();
317 * @brief Load a file into memory.
319 * @param filename the name of the file to be loaded.
320 * @param bufptr the place to store the newly allocated buffer pointer,
321 * if bufptr == NULL then the file is not actually loaded,
322 * just return the file size.
323 * The pointer *bufptr must be deleted afterwards.
324 * @return the size of the data actually loaded (mostly, it's the file size),
325 * zero means load failed.
327 EXAPI size_t scim_load_file (const String &filename, char **bufptr);
330 * @brief Make a directory.
332 * @param dir the dir path to be created.
334 * @return true if sucess.
336 EXAPI bool scim_make_dir (const String &dir);
339 * @brief Get the localized name of a language id.
340 * @param lang the language id.
341 * @return the localized name of this language, in utf8 encoding.
343 EXAPI String scim_get_language_name (const String &lang);
346 * @brief Get the English name of a language id.
347 * @param lang the language id.
348 * @return the English name of this language, in utf8 encoding.
350 EXAPI String scim_get_language_name_english (const String &lang);
353 * @brief Get the untranslated name of a language id.
354 * @param lang the language id.
355 * @return the untranslated name of this language, in utf8 encoding.
357 EXAPI String scim_get_language_name_untranslated (const String &lang);
360 * @brief Get the supported locales for a language.
362 * For example language zh_CN may support zh_CN.UTF-8, zh_CN.GB18030, zh_CN.GBK, zh_CN.GB2312 locales.
364 * @param lang the language id.
365 * @return the supported locales separated by comma.
367 EXAPI String scim_get_language_locales (const String &lang);
370 * @brief Get the language id for a locale.
371 * @param locale the locale name
372 * @return the language id for this locale.
374 EXAPI String scim_get_locale_language (const String &locale);
377 * @brief Test if the language is valid, and return the good language code.
378 * @param lang the language to be tested.
379 * @return If the language is valid, return the good language id, otherwise return "~other".
381 EXAPI String scim_validate_language (const String &lang);
384 * @brief Get the normalized language id of a language.
386 * Some short language id will be normalized to it's full id, for example:
391 * furthermore, zh_HK will be normalized to zh_TW, zh_SG will be normalized to zh_CN.
393 * @param lang the original language
394 * @return the normalized language code.
396 EXAPI String scim_get_normalized_language (const String &lang);
399 * @brief Launch a SCIM process with specific options.
401 * @param daemon If true then launch scim in a daemon process,
402 * otherwise the current process will be stopped until
403 * the newly created process exit.
404 * @param config The Config module to be used.
405 * @param imengines The IMEngines to be loaded, separated by comma.
406 * @param frontend The FrontEnd module to be used.
407 * @param argv Additional arguments passed to the new process's FrontEnd. Must
408 * terminated by a NULL pointer.
410 * @return Return 0 means the process started/exited without any problem, otherwise
411 * means an error occurred.
413 EXAPI int scim_launch (bool daemon,
414 const String &config,
415 const String &imengines,
416 const String &frontend,
417 char * const argv [] = 0);
420 * @brief Launch a SCIM Panel process with specific options.
422 * @param daemon If true then launch scim in a daemon process,
423 * otherwise the current process will be stopped until
424 * the newly created process exit.
425 * @param config The Config module to be used.
426 * @param display The display name on which the panel runs.
427 * eg. for X11 : localhost:0.0
428 * @param argv Additional arguments passed to the new process's FrontEnd. Must
429 * terminated by a NULL pointer.
431 * @return Return 0 means the process started/exited without any problem, otherwise
432 * means an error occurred.
434 EXAPI int scim_launch_panel (bool daemon,
435 const String &config,
436 const String &display,
437 char * const argv [] = 0);
440 * @brief Sleep some microseconds.
442 * @param usec The amount of microseconds to be sleeped.
444 EXAPI void scim_usleep (unsigned int usec);
447 * @brief Switch process into daemon mode.
449 EXAPI void scim_daemon ();
452 * @brief Save string to ISF log file.
454 * @param str The string to be saved.
456 EXAPI void isf_save_log (const char *fmt, ...);
458 #define ISF_SAVE_LOG(fmt, arg...) \
460 isf_save_log ("time:%ld pid:%d %s %s " fmt, time (0), getpid (), __FILE__, __func__, ##arg); \
465 void ISF_PROF_DEBUG_TIME(char const* func, int line, char const* str);
466 void ISF_PROF_DEBUG_TIME_BEGIN ();
467 void ISF_PROF_DEBUG_TIME_END (char const* format, char const* func, int line);
469 #define ISF_PROF_DEBUG(str) ISF_PROF_DEBUG_TIME(__FUNCTION__, __LINE__, str );
470 #define ISF_PROF_BEGIN() ISF_PROF_DEBUG_TIME_BEGIN ();
471 #define ISF_PROF_END(fmt, args...) \
473 ISF_PROF_DEBUG_TIME_END("[%s:%d]", __FUNCTION__, __LINE__); \
474 printf (fmt, ##args); \
479 #define ISF_PROF_DEBUG(str)
480 #define ISF_PROF_BEGIN()
481 #define ISF_PROF_END(fmt, ...)
483 #endif /* ISF_PROF */
485 //#define ENABLE_ISF_LOG 1
486 #ifdef ENABLE_ISF_LOG
487 #define ISF_LOG(fmt, args...) \
489 /*syslog (LOG_DEBUG, "[%s %d in %s ()]", __FILE__, __LINE__, __FUNCTION__); \
490 syslog (LOG_DEBUG, fmt, ##args); \ */
493 #define ISF_LOG(fmt, ...)
496 EXAPI void gettime (clock_t clock_start, const char* str);
498 typedef struct rectinfo
508 FIXED_CANDIDATE_WINDOW = 0,
509 FLOATING_CANDIDATE_WINDOW,
510 SOFT_CANDIDATE_WINDOW
511 } ISF_CANDIDATE_MODE_T;
515 ONE_LINE_CANDIDATE = 1,
517 THREE_LINE_CANDIDATE,
519 } ISF_CANDIDATE_PORTRAIT_LINE_T;
523 ISE_OPTIMIZATION_HINT_NONE = 0,
524 ISE_OPTIMIZATION_HINT_SHOW_PREPARE,
525 } ISE_OPTMIZATION_HINT_T;
527 #define PROP_X_EXT_KEYBOARD_INPUT_DETECTED "HW Keyboard Input Started"
528 #define PROP_X_EXT_KEYBOARD_EXIST "X External Keyboard Exist"
530 #define VCONFKEY_AUTOCAPITAL_ALLOW_BOOL "file/private/isf/autocapital_allow"
531 #define VCONFKEY_AUTOPERIOD_ALLOW_BOOL "file/private/isf/autoperiod_allow"
533 #define mzsc(m_code) "\x1b[" # m_code
534 #define mzc_normal mzsc(0m)
535 #define mzc_red mzsc(1;31m)
538 } /* namespace scim */
540 #endif /* __SCIM_UTILITY_H */
542 vi:ts=4:nowrap:ai:expandtab