12 * \brief gets the name of the system's terminal character set
13 * \return a malloced string indicating the system charset
15 * Be warned that this function on many systems is in no way thread-safe
16 * since it modifies global data
18 char* get_term_charset(void);
21 #if defined(FOR_MENCODER)
25 #include "gui/interface.h"
30 /* maximum message length of mp_msg */
31 #define MSGSIZE_MAX 3072
33 int mp_msg_levels
[MSGT_MAX
]; // verbose level of this module. initialized to -2
34 int mp_msg_level_all
= MSGL_STATUS
;
37 int mp_msg_module
= 0;
39 char *mp_msg_charset
= NULL
;
40 static char *old_charset
= NULL
;
41 static iconv_t msgiconv
;
44 const char* filename_recode(const char* filename
)
46 #if !defined(CONFIG_ICONV) || !defined(MSG_CHARSET)
49 static iconv_t inv_msgiconv
= (iconv_t
)(-1);
50 static char recoded_filename
[MSGSIZE_MAX
];
51 size_t filename_len
, max_path
;
53 if (!mp_msg_charset
||
54 !strcasecmp(mp_msg_charset
, MSG_CHARSET
) ||
55 !strcasecmp(mp_msg_charset
, "noconv"))
57 if (inv_msgiconv
== (iconv_t
)(-1)) {
58 inv_msgiconv
= iconv_open(MSG_CHARSET
, mp_msg_charset
);
59 if (inv_msgiconv
== (iconv_t
)(-1))
62 filename_len
= strlen(filename
);
63 max_path
= MSGSIZE_MAX
- 4;
64 precoded
= recoded_filename
;
65 if (iconv(inv_msgiconv
, &filename
, &filename_len
,
66 &precoded
, &max_path
) == (size_t)(-1) && errno
== E2BIG
) {
67 precoded
[0] = precoded
[1] = precoded
[2] = '.';
71 return recoded_filename
;
75 void mp_msg_init(void){
77 char *env
= getenv("MPLAYER_VERBOSE");
80 for(i
=0;i
<MSGT_MAX
;i
++) mp_msg_levels
[i
] = -2;
81 mp_msg_levels
[MSGT_IDENTIFY
] = -1; // no -identify output by default
83 mp_msg_charset
= getenv("MPLAYER_CHARSET");
85 mp_msg_charset
= get_term_charset();
89 int mp_msg_test(int mod
, int lev
)
91 return lev
<= (mp_msg_levels
[mod
] == -2 ? mp_msg_level_all
+ verbose
: mp_msg_levels
[mod
]);
94 static void set_msg_color(FILE* stream
, int lev
)
96 static const unsigned char v_colors
[10] = {9, 1, 3, 15, 7, 2, 2, 8, 8, 8};
97 int c
= v_colors
[lev
];
99 /* that's only a silly color test */
104 for(c
= 0; c
< 24; c
++)
105 printf("\033[%d;3%dm*** COLOR TEST %d ***\n", c
>7, c
&7, c
);
110 fprintf(stream
, "\033[%d;3%dm", c
>> 3, c
& 7);
113 static void print_msg_module(FILE* stream
, int mod
)
115 static const char *module_text
[MSGT_MAX
] = {
163 int c2
= (mod
+ 1) % 15 + 1;
168 fprintf(stream
, "\033[%d;3%dm", c2
>> 3, c2
& 7);
169 fprintf(stream
, "%9s", module_text
[mod
]);
171 fprintf(stream
, "\033[0;37m");
172 fprintf(stream
, ": ");
175 void mp_msg(int mod
, int lev
, const char *format
, ... ){
177 char tmp
[MSGSIZE_MAX
];
178 FILE *stream
= lev
<= MSGL_WARN
? stderr
: stdout
;
179 static int header
= 1;
181 if (!mp_msg_test(mod
, lev
)) return; // do not display
182 va_start(va
, format
);
183 vsnprintf(tmp
, MSGSIZE_MAX
, format
, va
);
185 tmp
[MSGSIZE_MAX
-2] = '\n';
186 tmp
[MSGSIZE_MAX
-1] = 0;
190 guiMessageBox(lev
, tmp
);
193 #if defined(CONFIG_ICONV) && defined(MSG_CHARSET)
194 if (mp_msg_charset
&& strcasecmp(mp_msg_charset
, "noconv")) {
195 char tmp2
[MSGSIZE_MAX
];
196 size_t inlen
= strlen(tmp
), outlen
= MSGSIZE_MAX
;
197 char *in
= tmp
, *out
= tmp2
;
198 if (!old_charset
|| strcmp(old_charset
, mp_msg_charset
)) {
201 iconv_close(msgiconv
);
203 msgiconv
= iconv_open(mp_msg_charset
, MSG_CHARSET
);
204 old_charset
= strdup(mp_msg_charset
);
206 if (msgiconv
== (iconv_t
)(-1)) {
207 fprintf(stderr
,"iconv: conversion from %s to %s unsupported\n"
208 ,MSG_CHARSET
,mp_msg_charset
);
210 memset(tmp2
, 0, MSGSIZE_MAX
);
211 while (iconv(msgiconv
, &in
, &inlen
, &out
, &outlen
) == -1) {
212 if (!inlen
|| !outlen
)
217 strncpy(tmp
, tmp2
, MSGSIZE_MAX
);
218 tmp
[MSGSIZE_MAX
-1] = 0;
219 tmp
[MSGSIZE_MAX
-2] = '\n';
225 print_msg_module(stream
, mod
);
226 set_msg_color(stream
, lev
);
227 header
= tmp
[strlen(tmp
)-1] == '\n' || tmp
[strlen(tmp
)-1] == '\r';
229 fprintf(stream
, "%s", tmp
);