~gabriel1984sibiu/mplayer/vaapi

1 by Grevutiu Gabriel
original source code upstream
1
/*
2
 * This file is part of MPlayer.
3
 *
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.
8
 *
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.
13
 *
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.
17
 */
18
19
#include <stdio.h>
20
#include <stdlib.h>
21
#include <stdarg.h>
22
#include <string.h>
23
#include <strings.h>
24
25
#include "config.h"
26
#include "osdep/getch2.h"
27
28
#ifdef CONFIG_ICONV
29
#include <iconv.h>
30
#include <errno.h>
31
#endif
32
33
#include "mp_msg.h"
34
35
/* maximum message length of mp_msg */
36
#define MSGSIZE_MAX 3072
37
38
int mp_msg_levels[MSGT_MAX]; // verbose level of this module. initialized to -2
39
int mp_msg_level_all = MSGL_STATUS;
40
int verbose = 0;
41
int mp_msg_color = 0;
42
int mp_msg_module = 0;
43
#ifdef CONFIG_ICONV
44
char *mp_msg_charset = NULL;
45
static char *old_charset = NULL;
46
static iconv_t msgiconv;
47
#endif
48
49
const char* filename_recode(const char* filename)
50
{
51
#if !defined(CONFIG_ICONV) || !defined(MSG_CHARSET)
52
    return filename;
53
#else
54
    static iconv_t inv_msgiconv = (iconv_t)(-1);
55
    static char recoded_filename[MSGSIZE_MAX];
56
    size_t filename_len, max_path;
57
    char* precoded;
58
    if (!mp_msg_charset ||
59
        !strcasecmp(mp_msg_charset, MSG_CHARSET) ||
60
        !strcasecmp(mp_msg_charset, "noconv"))
61
        return filename;
62
    if (inv_msgiconv == (iconv_t)(-1)) {
63
        inv_msgiconv = iconv_open(MSG_CHARSET, mp_msg_charset);
64
        if (inv_msgiconv == (iconv_t)(-1))
65
            return filename;
66
    }
67
    filename_len = strlen(filename);
68
    max_path = MSGSIZE_MAX - 4;
69
    precoded = recoded_filename;
70
    if (iconv(inv_msgiconv, &filename, &filename_len,
71
              &precoded, &max_path) == (size_t)(-1) && errno == E2BIG) {
72
        precoded[0] = precoded[1] = precoded[2] = '.';
73
        precoded += 3;
74
    }
75
    *precoded = '\0';
76
    return recoded_filename;
77
#endif
78
}
79
80
void mp_msg_init(void){
81
    int i;
82
    char *env = getenv("MPLAYER_VERBOSE");
83
    if (env)
84
        verbose = atoi(env);
85
    for(i=0;i<MSGT_MAX;i++) mp_msg_levels[i] = -2;
86
    mp_msg_levels[MSGT_IDENTIFY] = -1; // no -identify output by default
87
#ifdef CONFIG_ICONV
88
    mp_msg_charset = getenv("MPLAYER_CHARSET");
89
    if (!mp_msg_charset)
90
      mp_msg_charset = get_term_charset();
91
#endif
92
}
93
94
int mp_msg_test(int mod, int lev)
95
{
96
    return lev <= (mp_msg_levels[mod] == -2 ? mp_msg_level_all + verbose : mp_msg_levels[mod]);
97
}
98
99
static void set_msg_color(FILE* stream, int lev)
100
{
101
    static const unsigned char v_colors[10] = {9, 1, 3, 15, 7, 2, 2, 8, 8, 8};
102
    int c = v_colors[lev];
103
#ifdef MP_ANNOY_ME
104
    /* that's only a silly color test */
105
    {
106
        int c;
107
        static int flag = 1;
108
        if (flag)
109
            for(c = 0; c < 24; c++)
110
                printf("\033[%d;3%dm***  COLOR TEST %d  ***\n", c>7, c&7, c);
111
        flag = 0;
112
    }
113
#endif
114
    if (mp_msg_color)
115
        fprintf(stream, "\033[%d;3%dm", c >> 3, c & 7);
116
}
117
118
static void print_msg_module(FILE* stream, int mod)
119
{
120
    static const char *module_text[MSGT_MAX] = {
121
        "GLOBAL",
122
        "CPLAYER",
123
        "GPLAYER",
124
        "VIDEOOUT",
125
        "AUDIOOUT",
126
        "DEMUXER",
127
        "DS",
128
        "DEMUX",
129
        "HEADER",
130
        "AVSYNC",
131
        "AUTOQ",
132
        "CFGPARSER",
133
        "DECAUDIO",
134
        "DECVIDEO",
135
        "SEEK",
136
        "WIN32",
137
        "OPEN",
138
        "DVD",
139
        "PARSEES",
140
        "LIRC",
141
        "STREAM",
142
        "CACHE",
143
        "MENCODER",
144
        "XACODEC",
145
        "TV",
146
        "OSDEP",
147
        "SPUDEC",
148
        "PLAYTREE",
149
        "INPUT",
150
        "VFILTER",
151
        "OSD",
152
        "NETWORK",
153
        "CPUDETECT",
154
        "CODECCFG",
155
        "SWS",
156
        "VOBSUB",
157
        "SUBREADER",
158
        "AFILTER",
159
        "NETST",
160
        "MUXER",
161
        "OSDMENU",
162
        "IDENTIFY",
163
        "RADIO",
164
        "ASS",
165
        "LOADER",
166
        "STATUSLINE",
167
    };
168
    int c2 = (mod + 1) % 15 + 1;
169
170
    if (!mp_msg_module)
171
        return;
172
    if (mp_msg_color)
173
        fprintf(stream, "\033[%d;3%dm", c2 >> 3, c2 & 7);
174
    fprintf(stream, "%9s", module_text[mod]);
175
    if (mp_msg_color)
176
        fprintf(stream, "\033[0;37m");
177
    fprintf(stream, ": ");
178
}
179
180
void mp_msg(int mod, int lev, const char *format, ... ){
181
    va_list va;
182
    va_start(va, format);
183
    mp_msg_va(mod, lev, format, va);
184
    va_end(va);
185
}
186
187
void mp_msg_va(int mod, int lev, const char *format, va_list va){
188
    char tmp[MSGSIZE_MAX];
189
    FILE *stream = lev <= MSGL_WARN ? stderr : stdout;
190
    static int header = 1;
191
    // indicates if last line printed was a status line
192
    static int statusline;
193
    size_t len;
194
195
    if (!mp_msg_test(mod, lev)) return; // do not display
196
    vsnprintf(tmp, MSGSIZE_MAX, format, va);
197
    tmp[MSGSIZE_MAX-2] = '\n';
198
    tmp[MSGSIZE_MAX-1] = 0;
199
200
#if defined(CONFIG_ICONV) && defined(MSG_CHARSET)
201
    if (mp_msg_charset && strcasecmp(mp_msg_charset, "noconv")) {
202
      char tmp2[MSGSIZE_MAX];
203
      size_t inlen = strlen(tmp), outlen = MSGSIZE_MAX;
204
      char *in = tmp, *out = tmp2;
205
      if (!old_charset || strcmp(old_charset, mp_msg_charset)) {
206
        if (old_charset) {
207
          free(old_charset);
208
          iconv_close(msgiconv);
209
        }
210
        msgiconv = iconv_open(mp_msg_charset, MSG_CHARSET);
211
        old_charset = strdup(mp_msg_charset);
212
      }
213
      if (msgiconv == (iconv_t)(-1)) {
214
        fprintf(stderr,"iconv: conversion from %s to %s unsupported\n"
215
               ,MSG_CHARSET,mp_msg_charset);
216
      }else{
217
      memset(tmp2, 0, MSGSIZE_MAX);
218
      while (iconv(msgiconv, &in, &inlen, &out, &outlen) == -1) {
219
        if (!inlen || !outlen)
220
          break;
221
        *out++ = *in++;
222
        outlen--; inlen--;
223
      }
224
      strncpy(tmp, tmp2, MSGSIZE_MAX);
225
      tmp[MSGSIZE_MAX-1] = 0;
226
      tmp[MSGSIZE_MAX-2] = '\n';
227
      }
228
    }
229
#endif
230
231
    // as a status line normally is intended to be overwitten by next status line
232
    // output a '\n' to get a normal message on a separate line
233
    if (statusline && lev != MSGL_STATUS) fprintf(stream, "\n");
234
    statusline = lev == MSGL_STATUS;
235
236
    if (header)
237
        print_msg_module(stream, mod);
238
    set_msg_color(stream, lev);
239
    len = strlen(tmp);
240
    header = len && (tmp[len-1] == '\n' || tmp[len-1] == '\r');
241
242
    fprintf(stream, "%s", tmp);
243
    if (mp_msg_color)
244
        fprintf(stream, "\033[0m");
245
    fflush(stream);
246
}