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.
30 * \brief gets the name of the system's terminal character set
31 * \return a malloced string indicating the system charset
33 * Be warned that this function on many systems is in no way thread-safe
34 * since it modifies global data
36 char* get_term_charset(void);
39 #if defined(FOR_MENCODER)
43 #include "gui/interface.h"
48 /* maximum message length of mp_msg */
49 #define MSGSIZE_MAX 3072
51 int mp_msg_levels
[MSGT_MAX
]; // verbose level of this module. initialized to -2
52 int mp_msg_level_all
= MSGL_STATUS
;
55 int mp_msg_module
= 0;
57 char *mp_msg_charset
= NULL
;
58 static char *old_charset
= NULL
;
59 static iconv_t msgiconv
;
62 const char* filename_recode(const char* filename
)
64 #if !defined(CONFIG_ICONV) || !defined(MSG_CHARSET)
67 static iconv_t inv_msgiconv
= (iconv_t
)(-1);
68 static char recoded_filename
[MSGSIZE_MAX
];
69 size_t filename_len
, max_path
;
71 if (!mp_msg_charset
||
72 !strcasecmp(mp_msg_charset
, MSG_CHARSET
) ||
73 !strcasecmp(mp_msg_charset
, "noconv"))
75 if (inv_msgiconv
== (iconv_t
)(-1)) {
76 inv_msgiconv
= iconv_open(MSG_CHARSET
, mp_msg_charset
);
77 if (inv_msgiconv
== (iconv_t
)(-1))
80 filename_len
= strlen(filename
);
81 max_path
= MSGSIZE_MAX
- 4;
82 precoded
= recoded_filename
;
83 if (iconv(inv_msgiconv
, &filename
, &filename_len
,
84 &precoded
, &max_path
) == (size_t)(-1) && errno
== E2BIG
) {
85 precoded
[0] = precoded
[1] = precoded
[2] = '.';
89 return recoded_filename
;
93 void mp_msg_init(void){
95 char *env
= getenv("MPLAYER_VERBOSE");
98 for(i
=0;i
<MSGT_MAX
;i
++) mp_msg_levels
[i
] = -2;
99 mp_msg_levels
[MSGT_IDENTIFY
] = -1; // no -identify output by default
101 mp_msg_charset
= getenv("MPLAYER_CHARSET");
103 mp_msg_charset
= get_term_charset();
107 int mp_msg_test(int mod
, int lev
)
109 return lev
<= (mp_msg_levels
[mod
] == -2 ? mp_msg_level_all
+ verbose
: mp_msg_levels
[mod
]);
112 static void set_msg_color(FILE* stream
, int lev
)
114 static const unsigned char v_colors
[10] = {9, 1, 3, 15, 7, 2, 2, 8, 8, 8};
115 int c
= v_colors
[lev
];
117 /* that's only a silly color test */
122 for(c
= 0; c
< 24; c
++)
123 printf("\033[%d;3%dm*** COLOR TEST %d ***\n", c
>7, c
&7, c
);
128 fprintf(stream
, "\033[%d;3%dm", c
>> 3, c
& 7);
131 static void print_msg_module(FILE* stream
, int mod
)
133 static const char *module_text
[MSGT_MAX
] = {
181 int c2
= (mod
+ 1) % 15 + 1;
186 fprintf(stream
, "\033[%d;3%dm", c2
>> 3, c2
& 7);
187 fprintf(stream
, "%9s", module_text
[mod
]);
189 fprintf(stream
, "\033[0;37m");
190 fprintf(stream
, ": ");
193 void mp_msg(int mod
, int lev
, const char *format
, ... ){
195 char tmp
[MSGSIZE_MAX
];
196 FILE *stream
= lev
<= MSGL_WARN
? stderr
: stdout
;
197 static int header
= 1;
199 if (!mp_msg_test(mod
, lev
)) return; // do not display
200 va_start(va
, format
);
201 vsnprintf(tmp
, MSGSIZE_MAX
, format
, va
);
203 tmp
[MSGSIZE_MAX
-2] = '\n';
204 tmp
[MSGSIZE_MAX
-1] = 0;
208 guiMessageBox(lev
, tmp
);
211 #if defined(CONFIG_ICONV) && defined(MSG_CHARSET)
212 if (mp_msg_charset
&& strcasecmp(mp_msg_charset
, "noconv")) {
213 char tmp2
[MSGSIZE_MAX
];
214 size_t inlen
= strlen(tmp
), outlen
= MSGSIZE_MAX
;
215 char *in
= tmp
, *out
= tmp2
;
216 if (!old_charset
|| strcmp(old_charset
, mp_msg_charset
)) {
219 iconv_close(msgiconv
);
221 msgiconv
= iconv_open(mp_msg_charset
, MSG_CHARSET
);
222 old_charset
= strdup(mp_msg_charset
);
224 if (msgiconv
== (iconv_t
)(-1)) {
225 fprintf(stderr
,"iconv: conversion from %s to %s unsupported\n"
226 ,MSG_CHARSET
,mp_msg_charset
);
228 memset(tmp2
, 0, MSGSIZE_MAX
);
229 while (iconv(msgiconv
, &in
, &inlen
, &out
, &outlen
) == -1) {
230 if (!inlen
|| !outlen
)
235 strncpy(tmp
, tmp2
, MSGSIZE_MAX
);
236 tmp
[MSGSIZE_MAX
-1] = 0;
237 tmp
[MSGSIZE_MAX
-2] = '\n';
243 print_msg_module(stream
, mod
);
244 set_msg_color(stream
, lev
);
245 header
= tmp
[strlen(tmp
)-1] == '\n' || tmp
[strlen(tmp
)-1] == '\r';
247 fprintf(stream
, "%s", tmp
);