Consolidate all convert_* functions into libi3.

Some minor fixes along the way as well. Very minor stuff, unlikely
to ever be visible to the user.
This commit is contained in:
Fernando Tarlá Cardoso Lemos
2011-11-11 21:18:37 -02:00
committed by Michael Stapelberg
parent 061f24b247
commit fb11cc2d14
14 changed files with 129 additions and 288 deletions

View File

@ -51,7 +51,7 @@ static int sig_draw_window(xcb_window_t win, int width, int height, int font_hei
for (int i = 0; i < sizeof(crash_text) / sizeof(char*); i++) {
int text_len = strlen(crash_text[i]);
char *full_text = convert_utf8_to_ucs2(crash_text[i], &text_len);
xcb_char2b_t *full_text = convert_utf8_to_ucs2(crash_text[i], &text_len);
xcb_image_text_16(conn, text_len, pixmap, pixmap_gc, 8 /* X */,
3 + (i + 1) * font_height /* Y = baseline of font */,
(xcb_char2b_t*)full_text);
@ -151,7 +151,7 @@ void handle_signal(int sig, siginfo_t *info, void *data) {
/* calculate width for longest text */
int text_len = strlen(crash_text[crash_text_longest]);
char *longest_text = convert_utf8_to_ucs2(crash_text[crash_text_longest], &text_len);
xcb_char2b_t *longest_text = convert_utf8_to_ucs2(crash_text[crash_text_longest], &text_len);
int font_width = predict_text_width(longest_text, text_len);
int width = font_width + 20;

View File

@ -12,7 +12,6 @@
#include <sys/wait.h>
#include <stdarg.h>
#include <iconv.h>
#if defined(__OpenBSD__)
#include <sys/cdefs.h>
#endif
@ -24,8 +23,6 @@
#define SN_API_NOT_YET_FROZEN 1
#include <libsn/sn-launcher.h>
static iconv_t conversion_descriptor = 0;
int min(int a, int b) {
return (a < b ? a : b);
}
@ -120,51 +117,6 @@ void check_error(xcb_connection_t *conn, xcb_void_cookie_t cookie, char *err_mes
}
}
/*
* Converts the given string to UCS-2 big endian for use with
* xcb_image_text_16(). The amount of real glyphs is stored in real_strlen,
* a buffer containing the UCS-2 encoded string (16 bit per glyph) is
* returned. It has to be freed when done.
*
*/
char *convert_utf8_to_ucs2(char *input, int *real_strlen) {
size_t input_size = strlen(input) + 1;
/* UCS-2 consumes exactly two bytes for each glyph */
int buffer_size = input_size * 2;
char *buffer = smalloc(buffer_size);
size_t output_size = buffer_size;
/* We need to use an additional pointer, because iconv() modifies it */
char *output = buffer;
/* We convert the input into UCS-2 big endian */
if (conversion_descriptor == 0) {
conversion_descriptor = iconv_open("UCS-2BE", "UTF-8");
if (conversion_descriptor == 0) {
fprintf(stderr, "error opening the conversion context\n");
exit(1);
}
}
/* Get the conversion descriptor back to original state */
iconv(conversion_descriptor, NULL, NULL, NULL, NULL);
/* Convert our text */
int rc = iconv(conversion_descriptor, (void*)&input, &input_size, &output, &output_size);
if (rc == (size_t)-1) {
perror("Converting to UCS-2 failed");
FREE(buffer);
if (real_strlen != NULL)
*real_strlen = 0;
return NULL;
}
if (real_strlen != NULL)
*real_strlen = ((buffer_size - output_size) / 2) - 1;
return buffer;
}
/*
* This function resolves ~ in pathnames.
* It may resolve wildcards in the first part of the path, but if no match

View File

@ -69,7 +69,7 @@ void window_update_name(i3Window *win, xcb_get_property_reply_t *prop, bool befo
}
/* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
int len;
char *ucs2_name = convert_utf8_to_ucs2(new_name, &len);
xcb_char2b_t *ucs2_name = convert_utf8_to_ucs2(new_name, &len);
if (ucs2_name == NULL) {
LOG("Could not convert _NET_WM_NAME to UCS-2, ignoring new hint\n");
FREE(new_name);
@ -79,7 +79,7 @@ void window_update_name(i3Window *win, xcb_get_property_reply_t *prop, bool befo
FREE(win->name_x);
FREE(win->name_json);
win->name_json = new_name;
win->name_x = ucs2_name;
win->name_x = (char*)ucs2_name;
win->name_len = len;
win->name_x_changed = true;
LOG("_NET_WM_NAME changed to \"%s\"\n", win->name_json);

View File

@ -135,13 +135,13 @@ void xcb_raise_window(xcb_connection_t *conn, xcb_window_t window) {
* length (amount of glyphs) using the given font.
*
*/
int predict_text_width(char *text, int length) {
int predict_text_width(const xcb_char2b_t *text, int length) {
xcb_query_text_extents_cookie_t cookie;
xcb_query_text_extents_reply_t *reply;
xcb_generic_error_t *error;
int width;
cookie = xcb_query_text_extents(conn, config.font.id, length, (xcb_char2b_t*)text);
cookie = xcb_query_text_extents(conn, config.font.id, length, text);
if ((reply = xcb_query_text_extents_reply(conn, cookie, &error)) == NULL) {
ELOG("Could not get text extents (X error code %d)\n",
error->error_code);