]> git.sesse.net Git - vlc/blob - src/config/file.c
Remove config_GetUserConfDir
[vlc] / src / config / file.c
1 /*****************************************************************************
2  * file.c: configuration file handling
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <errno.h>                                                  /* errno */
29 #include <assert.h>
30 #include <limits.h>
31 #include <fcntl.h>
32 #include <sys/stat.h>
33 #ifdef __APPLE__
34 #   include <xlocale.h>
35 #else
36 #include <locale.h>
37 #endif
38
39 #include <vlc_common.h>
40 #include "../libvlc.h"
41 #include "vlc_charset.h"
42 #include "vlc_keys.h"
43
44 #include "configuration.h"
45 #include "modules/modules.h"
46
47 static char *ConfigKeyToString( int );
48
49 static inline char *strdupnull (const char *src)
50 {
51     return src ? strdup (src) : NULL;
52 }
53
54 /**
55  * Get the user's configuration file
56  */
57 static char *config_GetConfigFile( vlc_object_t *obj )
58 {
59     char *psz_file = config_GetPsz( obj, "config" );
60     if( psz_file == NULL )
61     {
62         char *psz_dir = config_GetUserDir( VLC_CONFIG_DIR );
63
64         if( asprintf( &psz_file, "%s" DIR_SEP CONFIG_FILE, psz_dir ) == -1 )
65             psz_file = NULL;
66         free( psz_dir );
67     }
68     return psz_file;
69 }
70
71 static FILE *config_OpenConfigFile( vlc_object_t *p_obj )
72 {
73     char *psz_filename = config_GetConfigFile( p_obj );
74     if( psz_filename == NULL )
75         return NULL;
76
77     msg_Dbg( p_obj, "opening config file (%s)", psz_filename );
78
79     FILE *p_stream = utf8_fopen( psz_filename, "rt" );
80     if( p_stream == NULL && errno != ENOENT )
81     {
82         msg_Err( p_obj, "cannot open config file (%s): %m",
83                  psz_filename );
84
85     }
86 #if !( defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS) )
87     else if( p_stream == NULL && errno == ENOENT )
88     {
89         /* This is the fallback for pre XDG Base Directory
90          * Specification configs */
91         char *home = config_GetUserDir(VLC_HOME_DIR);
92         char *psz_old;
93
94         if( home != NULL
95          && asprintf( &psz_old, "%s" DIR_SEP CONFIG_DIR DIR_SEP CONFIG_FILE,
96                       home ) != -1 )
97         {
98             p_stream = utf8_fopen( psz_old, "rt" );
99             if( p_stream )
100             {
101                 /* Old config file found. We want to write it at the
102                  * new location now. */
103                 msg_Info( p_obj->p_libvlc, "Found old config file at %s. "
104                           "VLC will now use %s.", psz_old, psz_filename );
105                 char *psz_readme;
106                 if( asprintf(&psz_readme,"%s"DIR_SEP CONFIG_DIR DIR_SEP"README",
107                              home ) != -1 )
108                 {
109                     FILE *p_readme = utf8_fopen( psz_readme, "wt" );
110                     if( p_readme )
111                     {
112                         fprintf( p_readme, "The VLC media player "
113                                  "configuration folder has moved to comply\n"
114                                  "with the XDG Base Directory Specification "
115                                  "version 0.6. Your\nconfiguration has been "
116                                  "copied to the new location:\n%s\nYou can "
117                                  "delete this directory and all its contents.",
118                                   psz_filename);
119                         fclose( p_readme );
120                     }
121                     free( psz_readme );
122                 }
123             }
124             free( psz_old );
125         }
126         free( home );
127     }
128 #endif
129     free( psz_filename );
130     return p_stream;
131 }
132
133
134 static int strtoi (const char *str)
135 {
136     char *end;
137     long l;
138
139     errno = 0;
140     l = strtol (str, &end, 0);
141
142     if (!errno)
143     {
144         if ((l > INT_MAX) || (l < INT_MIN))
145             errno = ERANGE;
146         if (*end)
147             errno = EINVAL;
148     }
149     return (int)l;
150 }
151
152
153 /*****************************************************************************
154  * config_LoadConfigFile: loads the configuration file.
155  *****************************************************************************
156  * This function is called to load the config options stored in the config
157  * file.
158  *****************************************************************************/
159 int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
160 {
161     FILE *file;
162
163     file = config_OpenConfigFile (p_this);
164     if (file == NULL)
165         return VLC_EGENERIC;
166
167     /* Look for the selected module, if NULL then save everything */
168     module_t **list = module_list_get (NULL);
169
170     /* Look for UTF-8 Byte Order Mark */
171     char * (*convert) (const char *) = strdupnull;
172     char bom[3];
173
174     if ((fread (bom, 1, 3, file) != 3)
175      || memcmp (bom, "\xEF\xBB\xBF", 3))
176     {
177         convert = FromLocaleDup;
178         rewind (file); /* no BOM, rewind */
179     }
180
181     module_t *module = NULL;
182     char line[1024], section[1022];
183     section[0] = '\0';
184
185     /* Ensure consistent number formatting... */
186     locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
187     locale_t baseloc = uselocale (loc);
188
189     while (fgets (line, 1024, file) != NULL)
190     {
191         /* Ignore comments and empty lines */
192         switch (line[0])
193         {
194             case '#':
195             case '\n':
196             case '\0':
197                 continue;
198         }
199
200         if (line[0] == '[')
201         {
202             char *ptr = strchr (line, ']');
203             if (ptr == NULL)
204                 continue; /* syntax error; */
205             *ptr = '\0';
206
207             /* New section ( = a given module) */
208             strcpy (section, line + 1);
209             module = NULL;
210
211             if ((psz_module_name == NULL)
212              || (strcmp (psz_module_name, section) == 0))
213             {
214                 for (int i = 0; list[i]; i++)
215                 {
216                     module_t *m = list[i];
217
218                     if ((strcmp (section, m->psz_object_name) == 0)
219                      && (m->i_config_items > 0)) /* ignore config-less modules */
220                     {
221                         module = m;
222                         if (psz_module_name != NULL)
223                             msg_Dbg (p_this,
224                                      "loading config for module \"%s\"",
225                                      section);
226                         break;
227                     }
228                 }
229             }
230
231             continue;
232         }
233
234         if (module == NULL)
235             continue; /* no need to parse if there is no matching module */
236
237         char *ptr = strchr (line, '\n');
238         if (ptr != NULL)
239             *ptr = '\0';
240
241         /* look for option name */
242         const char *psz_option_name = line;
243
244         ptr = strchr (line, '=');
245         if (ptr == NULL)
246             continue; /* syntax error */
247
248         *ptr = '\0';
249         const char *psz_option_value = ptr + 1;
250
251         /* try to match this option with one of the module's options */
252         for (size_t i = 0; i < module->confsize; i++)
253         {
254             module_config_t *p_item = module->p_config + i;
255
256             if ((p_item->i_type & CONFIG_HINT)
257              || strcmp (p_item->psz_name, psz_option_name))
258                 continue;
259
260             /* We found it */
261             errno = 0;
262
263             vlc_mutex_lock( p_item->p_lock );
264             switch( p_item->i_type )
265             {
266                 case CONFIG_ITEM_BOOL:
267                 case CONFIG_ITEM_INTEGER:
268                 {
269                     long l = strtoi (psz_option_value);
270                     if (errno)
271                         msg_Warn (p_this, "Integer value (%s) for %s: %m",
272                                   psz_option_value, psz_option_name);
273                     else
274                         p_item->saved.i = p_item->value.i = (int)l;
275                     break;
276                 }
277
278                 case CONFIG_ITEM_FLOAT:
279                     if( !*psz_option_value )
280                         break;                    /* ignore empty option */
281                     p_item->value.f = (float)atof (psz_option_value);
282                     p_item->saved.f = p_item->value.f;
283                     break;
284
285                 case CONFIG_ITEM_KEY:
286                     if( !*psz_option_value )
287                         break;                    /* ignore empty option */
288                     p_item->value.i = ConfigStringToKey(psz_option_value);
289                     p_item->saved.i = p_item->value.i;
290                     break;
291
292                 default:
293                     /* free old string */
294                     free( (char*) p_item->value.psz );
295                     free( (char*) p_item->saved.psz );
296
297                     p_item->value.psz = convert (psz_option_value);
298                     p_item->saved.psz = strdupnull (p_item->value.psz);
299                     break;
300             }
301             vlc_mutex_unlock( p_item->p_lock );
302             break;
303         }
304     }
305
306     if (ferror (file))
307     {
308         msg_Err (p_this, "error reading configuration: %m");
309         clearerr (file);
310     }
311     fclose (file);
312
313     module_list_free (list);
314     if (loc != (locale_t)0)
315     {
316         uselocale (baseloc);
317         freelocale (loc);
318     }
319     return 0;
320 }
321
322 /*****************************************************************************
323  * config_CreateDir: Create configuration directory if it doesn't exist.
324  *****************************************************************************/
325 int config_CreateDir( vlc_object_t *p_this, const char *psz_dirname )
326 {
327     if( !psz_dirname || !*psz_dirname ) return -1;
328
329     if( utf8_mkdir( psz_dirname, 0700 ) == 0 )
330         return 0;
331
332     switch( errno )
333     {
334         case EEXIST:
335             return 0;
336
337         case ENOENT:
338         {
339             /* Let's try to create the parent directory */
340             char psz_parent[strlen( psz_dirname ) + 1], *psz_end;
341             strcpy( psz_parent, psz_dirname );
342
343             psz_end = strrchr( psz_parent, DIR_SEP_CHAR );
344             if( psz_end && psz_end != psz_parent )
345             {
346                 *psz_end = '\0';
347                 if( config_CreateDir( p_this, psz_parent ) == 0 )
348                 {
349                     if( !utf8_mkdir( psz_dirname, 0700 ) )
350                         return 0;
351                 }
352             }
353         }
354     }
355
356     msg_Err( p_this, "could not create %s: %m", psz_dirname );
357     return -1;
358 }
359
360 static int
361 config_Write (FILE *file, const char *desc, const char *type,
362               bool comment, const char *name, const char *fmt, ...)
363 {
364     va_list ap;
365     int ret;
366
367     if (desc == NULL)
368         desc = "?";
369
370     if (fprintf (file, "# %s (%s)\n%s%s=", desc, vlc_gettext (type),
371                  comment ? "#" : "", name) < 0)
372         return -1;
373
374     va_start (ap, fmt);
375     ret = vfprintf (file, fmt, ap);
376     va_end (ap);
377     if (ret < 0)
378         return -1;
379
380     if (fputs ("\n\n", file) == EOF)
381         return -1;
382     return 0;
383 }
384
385
386 static int config_PrepareDir (vlc_object_t *obj)
387 {
388     char *psz_configdir = config_GetUserDir (VLC_CONFIG_DIR);
389     if (psz_configdir == NULL)
390         return -1;
391
392     int ret = config_CreateDir (obj, psz_configdir);
393     free (psz_configdir);
394     return ret;
395 }
396
397 /*****************************************************************************
398  * config_SaveConfigFile: Save a module's config options.
399  *****************************************************************************
400  * This will save the specified module's config options to the config file.
401  * If psz_module_name is NULL then we save all the modules config options.
402  * It's no use to save the config options that kept their default values, so
403  * we'll try to be a bit clever here.
404  *
405  * When we save we mustn't delete the config options of the modules that
406  * haven't been loaded. So we cannot just create a new config file with the
407  * config structures we've got in memory.
408  * I don't really know how to deal with this nicely, so I will use a completly
409  * dumb method ;-)
410  * I will load the config file in memory, but skipping all the sections of the
411  * modules we want to save. Then I will create a brand new file, dump the file
412  * loaded in memory and then append the sections of the modules we want to
413  * save.
414  * Really stupid no ?
415  *****************************************************************************/
416 static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
417                            bool b_autosave )
418 {
419     module_t *p_parser;
420     FILE *file = NULL;
421     char *permanent = NULL, *temporary = NULL;
422     char p_line[1024], *p_index2;
423     unsigned long i_sizebuf = 0;
424     char *p_bigbuffer = NULL, *p_index;
425     bool b_backup;
426     int i_index;
427
428     if( config_PrepareDir( p_this ) )
429     {
430         msg_Err( p_this, "no configuration directory" );
431         goto error;
432     }
433
434     file = config_OpenConfigFile( p_this );
435     if( file != NULL )
436     {
437         struct stat st;
438
439         /* Some users make vlcrc read-only to prevent changes.
440          * The atomic replacement scheme breaks this "feature",
441          * so we check for read-only by hand. */
442         if (fstat (fileno (file), &st)
443          || !(st.st_mode & S_IWUSR))
444         {
445             msg_Err (p_this, "configuration file is read-only");
446             goto error;
447         }
448         i_sizebuf = ( st.st_size < LONG_MAX ) ? st.st_size : 0;
449     }
450
451     p_bigbuffer = p_index = malloc( i_sizebuf+1 );
452     if( !p_bigbuffer )
453         goto error;
454     p_bigbuffer[0] = 0;
455
456     /* List all available modules */
457     module_t **list = module_list_get (NULL);
458
459     /* backup file into memory, we only need to backup the sections we won't
460      * save later on */
461     b_backup = false;
462     while( file && fgets( p_line, 1024, file ) )
463     {
464         if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
465         {
466
467             /* we found a section, check if we need to do a backup */
468             for( i_index = 0; (p_parser = list[i_index]) != NULL; i_index++ )
469             {
470                 if( ((p_index2 - &p_line[1])
471                        == (int)strlen(p_parser->psz_object_name) )
472                     && !memcmp( &p_line[1], p_parser->psz_object_name,
473                                 strlen(p_parser->psz_object_name) ) )
474                 {
475                     if( !psz_module_name )
476                         break;
477                     else if( !strcmp( psz_module_name,
478                                       p_parser->psz_object_name ) )
479                         break;
480                 }
481             }
482
483             if( list[i_index] == NULL )
484             {
485                 /* we don't have this section in our list so we need to back
486                  * it up */
487                 *p_index2 = 0;
488 #if 0
489                 msg_Dbg( p_this, "backing up config for unknown module \"%s\"",
490                                  &p_line[1] );
491 #endif
492                 *p_index2 = ']';
493
494                 b_backup = true;
495             }
496             else
497             {
498                 b_backup = false;
499             }
500         }
501
502         /* save line if requested and line is valid (doesn't begin with a
503          * space, tab, or eol) */
504         if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
505             && (p_line[0] != '\t') )
506         {
507             strcpy( p_index, p_line );
508             p_index += strlen( p_line );
509         }
510     }
511     if( file )
512         fclose( file );
513     file = NULL;
514
515     /*
516      * Save module config in file
517      */
518     permanent = config_GetConfigFile (p_this);
519     if (!permanent)
520     {
521         module_list_free (list);
522         goto error;
523     }
524
525     if (asprintf (&temporary, "%s.%u", permanent,
526 #ifdef UNDER_CE
527                   GetCurrentProcessId ()
528 #else
529                   getpid ()
530 #endif
531                  ) == -1)
532     {
533         temporary = NULL;
534         module_list_free (list);
535         goto error;
536     }
537
538     /* The temporary configuration file is per-PID. Therefore SaveConfigFile()
539      * should be serialized against itself within a given process. */
540     static vlc_mutex_t lock = VLC_STATIC_MUTEX;
541     vlc_mutex_lock (&lock);
542
543     int fd = utf8_open (temporary, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);
544     if (fd == -1)
545     {
546         vlc_mutex_unlock (&lock);
547         module_list_free (list);
548         goto error;
549     }
550     file = fdopen (fd, "wt");
551     if (file == NULL)
552     {
553         close (fd);
554         vlc_mutex_unlock (&lock);
555         module_list_free (list);
556         goto error;
557     }
558
559     fprintf( file, "\xEF\xBB\xBF###\n###  " COPYRIGHT_MESSAGE "\n###\n\n"
560        "###\n### lines beginning with a '#' character are comments\n###\n\n" );
561
562     /* Ensure consistent number formatting... */
563     locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
564     locale_t baseloc = uselocale (loc);
565
566     /* Look for the selected module, if NULL then save everything */
567     for( i_index = 0; (p_parser = list[i_index]) != NULL; i_index++ )
568     {
569         module_config_t *p_item, *p_end;
570
571         if( psz_module_name && strcmp( psz_module_name,
572                                        p_parser->psz_object_name ) )
573             continue;
574
575         if( !p_parser->i_config_items )
576             continue;
577
578         if( psz_module_name )
579             msg_Dbg( p_this, "saving config for module \"%s\"",
580                      p_parser->psz_object_name );
581
582         fprintf( file, "[%s]", p_parser->psz_object_name );
583         if( p_parser->psz_longname )
584             fprintf( file, " # %s\n\n", p_parser->psz_longname );
585         else
586             fprintf( file, "\n\n" );
587
588         for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
589              p_item < p_end;
590              p_item++ )
591         {
592             if ((p_item->i_type & CONFIG_HINT) /* ignore hint */
593              || p_item->b_removed              /* ignore deprecated option */
594              || p_item->b_unsaveable)          /* ignore volatile option */
595                 continue;
596
597             vlc_mutex_lock (p_item->p_lock);
598
599             /* Do not save the new value in the configuration file
600              * if doing an autosave, and the item is not an "autosaved" one. */
601             bool b_retain = b_autosave && !p_item->b_autosave;
602
603             if (IsConfigIntegerType (p_item->i_type))
604             {
605                 int val = b_retain ? p_item->saved.i : p_item->value.i;
606                 if (p_item->i_type == CONFIG_ITEM_KEY)
607                 {
608                     char *psz_key = ConfigKeyToString (val);
609                     config_Write (file, p_item->psz_text, N_("key"),
610                                   val == p_item->orig.i,
611                                   p_item->psz_name, "%s",
612                                   psz_key ? psz_key : "");
613                     free (psz_key);
614                 }
615                 else
616                     config_Write (file, p_item->psz_text,
617                                   (p_item->i_type == CONFIG_ITEM_BOOL)
618                                       ? N_("boolean") : N_("integer"),
619                                   val == p_item->orig.i,
620                                   p_item->psz_name, "%d", val);
621                 p_item->saved.i = val;
622             }
623             else
624             if (IsConfigFloatType (p_item->i_type))
625             {
626                 float val = b_retain ? p_item->saved.f : p_item->value.f;
627                 config_Write (file, p_item->psz_text, N_("float"),
628                               val == p_item->orig.f,
629                               p_item->psz_name, "%f", val);
630                 p_item->saved.f = val;
631             }
632             else
633             {
634                 const char *psz_value = b_retain ? p_item->saved.psz
635                                                  : p_item->value.psz;
636                 bool modified;
637
638                 assert (IsConfigStringType (p_item->i_type));
639
640                 if (b_retain && (psz_value == NULL)) /* FIXME: hack */
641                     psz_value = p_item->orig.psz;
642
643                 modified =
644                     (psz_value != NULL)
645                         ? ((p_item->orig.psz != NULL)
646                             ? (strcmp (psz_value, p_item->orig.psz) != 0)
647                             : true)
648                         : (p_item->orig.psz != NULL);
649
650                 config_Write (file, p_item->psz_text, N_("string"),
651                               !modified, p_item->psz_name, "%s",
652                               psz_value ? psz_value : "");
653
654                 if ( !b_retain )
655                 {
656
657                     free ((char *)p_item->saved.psz);
658                     if( (psz_value && p_item->orig.psz &&
659                          strcmp( psz_value, p_item->orig.psz )) ||
660                         !psz_value || !p_item->orig.psz)
661                         p_item->saved.psz = strdupnull (psz_value);
662                     else
663                         p_item->saved.psz = NULL;
664                 }
665             }
666
667             if (!b_retain)
668                 p_item->b_dirty = false;
669             vlc_mutex_unlock (p_item->p_lock);
670         }
671     }
672
673     module_list_free (list);
674     if (loc != (locale_t)0)
675     {
676         uselocale (baseloc);
677         freelocale (loc);
678     }
679
680     /*
681      * Restore old settings from the config in file
682      */
683     fputs( p_bigbuffer, file );
684     free( p_bigbuffer );
685
686     /*
687      * Flush to disk and replace atomically
688      */
689     fflush (file); /* Flush from run-time */
690 #ifndef WIN32
691     fdatasync (fd); /* Flush from OS */
692     /* Atomically replace the file... */
693     if (utf8_rename (temporary, permanent))
694         utf8_unlink (temporary);
695     /* (...then synchronize the directory, err, TODO...) */
696     /* ...and finally close the file */
697     vlc_mutex_unlock (&lock);
698 #endif
699     fclose (file);
700 #ifdef WIN32
701     /* Windows cannot remove open files nor overwrite existing ones */
702     utf8_unlink (permanent);
703     if (utf8_rename (temporary, permanent))
704         utf8_unlink (temporary);
705     vlc_mutex_unlock (&lock);
706 #endif
707
708     free (temporary);
709     free (permanent);
710     return 0;
711
712 error:
713     if( file )
714         fclose( file );
715     free (temporary);
716     free (permanent);
717     free( p_bigbuffer );
718     return -1;
719 }
720
721 int config_AutoSaveConfigFile( vlc_object_t *p_this )
722 {
723     size_t i_index;
724     bool save = false;
725
726     assert( p_this );
727
728     /* Check if there's anything to save */
729     module_t **list = module_list_get (NULL);
730     for( i_index = 0; list[i_index] && !save; i_index++ )
731     {
732         module_t *p_parser = list[i_index];
733         module_config_t *p_item, *p_end;
734
735         if( !p_parser->i_config_items ) continue;
736
737         for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
738              p_item < p_end && !save;
739              p_item++ )
740         {
741             vlc_mutex_lock (p_item->p_lock);
742             save = p_item->b_autosave && p_item->b_dirty;
743             vlc_mutex_unlock (p_item->p_lock);
744         }
745     }
746     module_list_free (list);
747
748     return save ? VLC_SUCCESS : SaveConfigFile( p_this, NULL, true );
749 }
750
751 int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
752 {
753     return SaveConfigFile( p_this, psz_module_name, false );
754 }
755
756 int ConfigStringToKey( const char *psz_key )
757 {
758     int i_key = 0;
759     size_t i;
760     const char *psz_parser = strchr( psz_key, '-' );
761     while( psz_parser && psz_parser != psz_key )
762     {
763         for( i = 0; i < vlc_num_modifiers; ++i )
764         {
765             if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
766                               strlen( vlc_modifiers[i].psz_key_string ) ) )
767             {
768                 i_key |= vlc_modifiers[i].i_key_code;
769             }
770         }
771         psz_key = psz_parser + 1;
772         psz_parser = strchr( psz_key, '-' );
773     }
774     for( i = 0; i < vlc_num_keys; ++i )
775     {
776         if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
777         {
778             i_key |= vlc_keys[i].i_key_code;
779             break;
780         }
781     }
782     return i_key;
783 }
784
785 char *ConfigKeyToString( int i_key )
786 {
787     // Worst case appears to be 45 characters:
788     // "Command-Meta-Ctrl-Shift-Alt-Browser Favorites"
789     enum { keylen=64 };
790     char *psz_key = malloc( keylen );
791     char *p;
792     size_t index;
793
794     if ( !psz_key )
795     {
796         return NULL;
797     }
798     *psz_key = '\0';
799     p = psz_key;
800
801     for( index = 0; index < vlc_num_modifiers; ++index )
802     {
803         if( i_key & vlc_modifiers[index].i_key_code )
804         {
805             p += snprintf( p, keylen-(psz_key-p), "%s-",
806                            vlc_modifiers[index].psz_key_string );
807         }
808     }
809     for( index = 0; index < vlc_num_keys; ++index )
810     {
811         if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
812         {
813             p += snprintf( p, keylen-(psz_key-p), "%s",
814                            vlc_keys[index].psz_key_string );
815             break;
816         }
817     }
818     return psz_key;
819 }
820