]> git.sesse.net Git - vlc/commitdiff
ncurses: Use ncursesw to correctly display wide characters on UTF-8 locale
authorRafaël Carré <funman@videolan.org>
Thu, 13 Sep 2007 17:53:40 +0000 (17:53 +0000)
committerRafaël Carré <funman@videolan.org>
Thu, 13 Sep 2007 17:53:40 +0000 (17:53 +0000)
NEWS
configure.ac
modules/gui/ncurses.c

diff --git a/NEWS b/NEWS
index 9018683918905b48a283b8327d3a0b5f6a98f25a..f687d3fe4491fd99d713f6aad1d9622611abb351 100644 (file)
--- 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
index 965ac93c9697796c84079698783bbdf74972c4e8..911eaf02a8e252ddd827125772f4b34fef417219 100644 (file)
@@ -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
index cbe02f81048e5c0f89b9afdd3f04c481a5471130..c4c1f580d06ee3786252892fd4c1e43cd5748b57 100644 (file)
@@ -32,7 +32,7 @@
 #include <errno.h>                                                 /* ENOMEM */
 #include <time.h>
 
-#include <curses.h>
+#include <ncursesw/curses.h>
 
 #include <vlc_interface.h>
 #include <vlc_vout.h>
@@ -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, ... )