From bfa9e5bbeb20aa3e734c9a5629f139a973154a9b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rafa=C3=ABl=20Carr=C3=A9?= Date: Thu, 13 Sep 2007 17:53:40 +0000 Subject: [PATCH] ncurses: Use ncursesw to correctly display wide characters on UTF-8 locale --- NEWS | 2 ++ configure.ac | 2 +- modules/gui/ncurses.c | 73 +++++++++++++++++++++++++------------------ 3 files changed, 46 insertions(+), 31 deletions(-) diff --git a/NEWS b/NEWS index 9018683918..f687d3fe44 100644 --- a/NEWS +++ b/NEWS @@ -111,6 +111,8 @@ Interfaces: interface for media players that intends to become an xdg standard when finished: http://wiki.xmms2.xmms.se/index.php/Media_Player_Interfaces . * Motion module use disk accelerometers to keep video horizontal + * Ncurses interface now uses ncursesw to correctly display wide characters + when using an UTF-8 locale. Linux Port: * VLC now complies with the XDG Base Directory Specification version 0.6 diff --git a/configure.ac b/configure.ac index 965ac93c96..911eaf02a8 100644 --- a/configure.ac +++ b/configure.ac @@ -5313,7 +5313,7 @@ AC_ARG_ENABLE(ncurses, [ --enable-ncurses ncurses interface support (default disabled)], [if test "${enable_ncurses}" = "yes"; then VLC_ADD_PLUGINS([ncurses]) - VLC_ADD_LDFLAGS([ncurses],[-lncurses]) + VLC_ADD_LDFLAGS([ncurses],[-lncursesw]) fi]) dnl diff --git a/modules/gui/ncurses.c b/modules/gui/ncurses.c index cbe02f8104..c4c1f580d0 100644 --- a/modules/gui/ncurses.c +++ b/modules/gui/ncurses.c @@ -32,7 +32,7 @@ #include /* ENOMEM */ #include -#include +#include #include #include @@ -1125,46 +1125,59 @@ static void mvnprintw( int y, int x, int w, const char *p_fmt, ... ) va_list vl_args; char *p_buf = NULL; int i_len; + size_t i_char_len; /* UCS character length */ + size_t i_width; /* Display width */ + wchar_t *psz_wide; /* wchar_t representation of p_buf */ va_start( vl_args, p_fmt ); vasprintf( &p_buf, p_fmt, vl_args ); va_end( vl_args ); - if( p_buf == NULL ) - { + if( ( p_buf == NULL ) || ( w <= 0 ) ) return; - } - if( w > 0 ) + + i_len = strlen( p_buf ); + psz_wide = (wchar_t *) malloc( sizeof( wchar_t ) * ( i_len + 1 ) ); + + i_char_len = mbstowcs( psz_wide, p_buf, i_len ); + + if( i_char_len == -1 ) /* an invalid character was encountered */ + i_width = i_len; + else { - if( ( i_len = strlen( p_buf ) ) > w ) - { - char *psz_local; - int i_cut = i_len - w; - int x1 = i_len/2 - i_cut/2; - int x2 = x1 + i_cut; + i_width = wcswidth( psz_wide, i_char_len ); + if( i_width == -1 ) /* a non printable character was encountered */ + i_width = i_len; + } - if( i_len > x2 ) - { - memmove( &p_buf[x1], &p_buf[x2], i_len - x2 ); - } - p_buf[w] = '\0'; - if( w > 7 ) - { - p_buf[w/2-1] = '.'; - p_buf[w/2 ] = '.'; - p_buf[w/2+1] = '.'; - } - psz_local = ToLocale( p_buf ); - mvprintw( y, x, "%s", psz_local ); - LocaleFree( p_buf ); + if( i_width > w ) + { /* FIXME: ellipsize psz_wide while keeping the width in mind */ + char *psz_local; + int i_cut = i_len - w; + int x1 = i_len/2 - i_cut/2; + int x2 = x1 + i_cut; + + if( i_len > x2 ) + { + memmove( &p_buf[x1], &p_buf[x2], i_len - x2 ); } - else + p_buf[w] = '\0'; + if( w > 7 ) { - char *psz_local = ToLocale( p_buf ); - mvprintw( y, x, "%s", psz_local ); - LocaleFree( p_buf ); - mvhline( y, x + i_len, ' ', w - i_len ); + p_buf[w/2-1] = '.'; + p_buf[w/2 ] = '.'; + p_buf[w/2+1] = '.'; } + psz_local = ToLocale( p_buf ); + mvprintw( y, x, "%s", psz_local ); + LocaleFree( p_buf ); + } + else + { + char *psz_local = ToLocale( p_buf ); + mvprintw( y, x, "%s", psz_local ); + LocaleFree( p_buf ); + mvhline( y, x + i_width, ' ', w - i_width ); } } static void MainBoxWrite( intf_thread_t *p_intf, int l, int x, const char *p_fmt, ... ) -- 2.39.5