2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "osdep/getch2.h"
27 #ifdef CONFIG_TRANSLATION
39 /* maximum message length of mp_msg */
40 #define MSGSIZE_MAX 3072
42 int mp_msg_levels
[MSGT_MAX
]; // verbose level of this module. initialized to -2
43 int mp_msg_level_all
= MSGL_STATUS
;
46 int mp_msg_module
= 0;
48 char *mp_msg_charset
= NULL
;
49 static char *old_charset
= NULL
;
50 static iconv_t msgiconv
;
53 const char* filename_recode(const char* filename
)
55 #if !defined(CONFIG_ICONV) || !defined(MSG_CHARSET)
58 static iconv_t inv_msgiconv
= (iconv_t
)(-1);
59 static char recoded_filename
[MSGSIZE_MAX
];
60 size_t filename_len
, max_path
;
62 if (!mp_msg_charset
||
63 !strcasecmp(mp_msg_charset
, MSG_CHARSET
) ||
64 !strcasecmp(mp_msg_charset
, "noconv"))
66 if (inv_msgiconv
== (iconv_t
)(-1)) {
67 inv_msgiconv
= iconv_open(MSG_CHARSET
, mp_msg_charset
);
68 if (inv_msgiconv
== (iconv_t
)(-1))
71 filename_len
= strlen(filename
);
72 max_path
= MSGSIZE_MAX
- 4;
73 precoded
= recoded_filename
;
74 if (iconv(inv_msgiconv
, &filename
, &filename_len
,
75 &precoded
, &max_path
) == (size_t)(-1) && errno
== E2BIG
) {
76 precoded
[0] = precoded
[1] = precoded
[2] = '.';
80 return recoded_filename
;
84 void mp_msg_init(void){
86 char *env
= getenv("MPLAYER_VERBOSE");
89 for(i
=0;i
<MSGT_MAX
;i
++) mp_msg_levels
[i
] = -2;
90 mp_msg_levels
[MSGT_IDENTIFY
] = -1; // no -identify output by default
92 mp_msg_charset
= getenv("MPLAYER_CHARSET");
94 mp_msg_charset
= get_term_charset();
96 #ifdef CONFIG_TRANSLATION
97 textdomain("mplayer");
98 char *localedir
= getenv("MPLAYER_LOCALEDIR");
99 if (localedir
== NULL
&& strlen(MPLAYER_LOCALEDIR
))
100 localedir
= MPLAYER_LOCALEDIR
;
101 bindtextdomain("mplayer", localedir
);
102 bind_textdomain_codeset("mplayer", "UTF-8");
106 int mp_msg_test(int mod
, int lev
)
108 return lev
<= (mp_msg_levels
[mod
] == -2 ? mp_msg_level_all
+ verbose
: mp_msg_levels
[mod
]);
111 static void set_msg_color(FILE* stream
, int lev
)
113 static const unsigned char v_colors
[10] = {9, 1, 3, 15, 7, 2, 2, 8, 8, 8};
114 int c
= v_colors
[lev
];
116 /* that's only a silly color test */
121 for(c
= 0; c
< 24; c
++)
122 printf("\033[%d;3%dm*** COLOR TEST %d ***\n", c
>7, c
&7, c
);
127 fprintf(stream
, "\033[%d;3%dm", c
>> 3, c
& 7);
130 static void print_msg_module(FILE* stream
, int mod
)
132 static const char *module_text
[MSGT_MAX
] = {
180 int c2
= (mod
+ 1) % 15 + 1;
185 fprintf(stream
, "\033[%d;3%dm", c2
>> 3, c2
& 7);
186 fprintf(stream
, "%9s", module_text
[mod
]);
188 fprintf(stream
, "\033[0;37m");
189 fprintf(stream
, ": ");
192 void mp_msg_va(int mod
, int lev
, const char *format
, va_list va
)
194 char tmp
[MSGSIZE_MAX
];
195 FILE *stream
= lev
<= MSGL_WARN
? stderr
: stdout
;
196 static int header
= 1;
198 if (!mp_msg_test(mod
, lev
)) return; // do not display
199 vsnprintf(tmp
, MSGSIZE_MAX
, format
, va
);
200 tmp
[MSGSIZE_MAX
-2] = '\n';
201 tmp
[MSGSIZE_MAX
-1] = 0;
203 #if defined(CONFIG_ICONV) && defined(MSG_CHARSET)
204 if (mp_msg_charset
&& strcasecmp(mp_msg_charset
, "noconv")) {
205 char tmp2
[MSGSIZE_MAX
];
206 size_t inlen
= strlen(tmp
), outlen
= MSGSIZE_MAX
;
207 char *in
= tmp
, *out
= tmp2
;
208 if (!old_charset
|| strcmp(old_charset
, mp_msg_charset
)) {
211 iconv_close(msgiconv
);
213 msgiconv
= iconv_open(mp_msg_charset
, MSG_CHARSET
);
214 old_charset
= strdup(mp_msg_charset
);
216 if (msgiconv
== (iconv_t
)(-1)) {
217 fprintf(stderr
,"iconv: conversion from %s to %s unsupported\n"
218 ,MSG_CHARSET
,mp_msg_charset
);
220 memset(tmp2
, 0, MSGSIZE_MAX
);
221 while (iconv(msgiconv
, &in
, &inlen
, &out
, &outlen
) == -1) {
222 if (!inlen
|| !outlen
)
227 strncpy(tmp
, tmp2
, MSGSIZE_MAX
);
228 tmp
[MSGSIZE_MAX
-1] = 0;
229 tmp
[MSGSIZE_MAX
-2] = '\n';
235 print_msg_module(stream
, mod
);
236 set_msg_color(stream
, lev
);
237 header
= tmp
[strlen(tmp
)-1] == '\n' || tmp
[strlen(tmp
)-1] == '\r';
239 fprintf(stream
, "%s", tmp
);
241 fprintf(stream
, "\033[0m");
245 void mp_msg(int mod
, int lev
, const char *format
, ...)
248 va_start(va
, format
);
249 mp_msg_va(mod
, lev
, format
, va
);
253 char *mp_gtext(const char *string
)
255 #ifdef CONFIG_TRANSLATION
256 /* gettext expects the global locale to be set with
257 * setlocale(LC_ALL, ""). However doing that would suck for a
258 * couple of reasons (locale stuff is badly designed and sucks in
261 * First setting the locale, especially LC_CTYPE, changes the
262 * behavior of various C functions and we don't want that - we
263 * want isalpha() for example to always behave like in the C
266 * Second, there is no way to enforce a sane character set. All
267 * strings inside MPlayer must always be in utf-8, not in the
268 * character set specified by the system locale which could be
269 * something different and completely insane. The locale system
270 * lacks any way to say "set LC_CTYPE to utf-8, ignoring the
271 * default system locale if it specifies something different". We
272 * could try to work around that flaw by leaving LC_CTYPE to the C
273 * locale and only setting LC_MESSAGES (which is the variable that
274 * must be set to tell gettext which language to translate
275 * to). However if we leave LC_MESSAGES set then things like
276 * strerror() may produce completely garbled output when they try
277 * to translate their results but then try to convert some
278 * translated non-ASCII text to the character set specified by
279 * LC_CTYPE which would still be in the C locale (this doesn't
280 * affect gettext itself because it supports specifying the
281 * character set directly with bind_textdomain_codeset()).
283 * So the only solution (at leat short of trying to work around
284 * things possibly producing non-utf-8 output) is to leave all the
285 * locale variables unset. Note that this means it's not possible
286 * to get translated output from any libraries we call if they
287 * only rely on the broken locale system to specify the language
288 * to use; this is the case with libc for example.
290 * The locale changing below is rather ugly, but hard to avoid.
291 * gettext doesn't support specifying the translation target
292 * directly, only through locale.
293 * The main actual problem this could cause is interference with
294 * other threads; that could be avoided with thread-specific
295 * locale changes, but such functionality is less standard and I
296 * think it's not worth adding pre-emptively unless someone sees
297 * an actual problem case.
299 setlocale(LC_MESSAGES
, "");
300 string
= gettext(string
);
301 setlocale(LC_MESSAGES
, "C");
306 void mp_tmsg(int mod
, int lev
, const char *format
, ...)
309 va_start(va
, format
);
310 mp_msg_va(mod
, lev
, mp_gtext(format
), va
);