cca2105bf22827ac5ebe8d8d1e048200d088cc13
[platform/upstream/gstreamer.git] / tools / gst-register.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gst-register.c: Plugin subsystem for loading elements, types, and libs
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <gst/gst.h>
28
29 #include <stdlib.h>
30 #include <stdio.h>
31
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <locale.h>
37
38 extern gboolean _gst_registry_auto_load;
39 static gint num_features = 0;
40 static gint num_plugins = 0;
41
42 static void
43 plugin_added_func (GstRegistry *registry, GstPlugin *plugin, gpointer user_data)
44 {
45   g_print ("added plugin %s with %d feature(s)\n", plugin->desc.name,
46            plugin->numfeatures);
47
48   num_features += plugin->numfeatures;
49   num_plugins++;
50 }
51
52 static void
53 spawn_all_in_dir(const char *dirname)
54 {
55   char *argv[2] = { NULL, NULL };
56   GDir *dir;
57   const char *file;
58
59   /* g_print("spawning all in %s\n", dirname); */
60
61   dir = g_dir_open (dirname, 0, NULL);
62   if (dir == NULL) return;
63
64   while( (file = g_dir_read_name (dir)) ){
65     argv[0] = g_build_filename(dirname, file, NULL);
66     g_print("running %s\n",argv[0]);
67     g_spawn_sync (NULL, argv, NULL, G_SPAWN_FILE_AND_ARGV_ZERO, NULL, NULL, NULL, NULL, NULL, NULL);
68     g_free(argv[0]);
69   }
70   g_dir_close(dir);
71 }
72
73 int main (int argc,char *argv[])
74 {
75   GList *registries;
76   GList *path_spill = NULL; /* used for path spill from failing registries */
77
78   setlocale(LC_ALL, "");
79
80     /* Init gst */
81   _gst_registry_auto_load = FALSE;
82   gst_init (&argc, &argv);
83
84   registries = gst_registry_pool_list ();
85   registries = g_list_reverse (registries);
86
87   while (registries) {
88     GstRegistry *registry = GST_REGISTRY (registries->data);
89     GList *dir_list;
90     GList *iter;
91     char *dir;
92
93     if (path_spill)
94     {
95       GList *iter;
96
97       /* add spilled paths to this registry;
98        * since they're spilled they probably weren't loaded correctly
99        * so we should give a lower priority registry the chance to do them */
100       for (iter = path_spill; iter; iter = iter->next)
101       {
102         g_print ("added path   %s to %s \n",
103                  (const char *) iter->data, registry->name);
104         gst_registry_add_path (registry, (const gchar *) iter->data);
105       }
106       g_list_free (path_spill);
107       path_spill = NULL;
108     }
109
110     g_signal_connect (G_OBJECT (registry), "plugin_added",
111                       G_CALLBACK (plugin_added_func), NULL);
112
113     if (registry->flags & GST_REGISTRY_WRITABLE) {
114       char *location;
115       g_object_get (registry, "location", &location, NULL);
116       g_print ("rebuilding %s (%s)\n", registry->name, location);
117       g_free (location);
118       gst_registry_rebuild (registry);
119       gst_registry_save (registry);
120     }
121     else {
122       g_print ("trying to load %s\n", registry->name);
123       if (!gst_registry_load (registry))
124       {
125         g_print ("error loading %s\n", registry->name);
126         /* move over paths from this registry to the next one */
127         path_spill = g_list_concat (path_spill,
128                                     gst_registry_get_path_list (registry));
129         g_assert (path_spill != NULL);
130       }
131       /* also move over paths if the registry wasn't writable
132        * FIXME: we should check if the paths that were loaded from this
133          registry get removed from the path_list so we only try to
134          spill paths that could not be registered */
135       path_spill = g_list_concat (path_spill,
136                                   gst_registry_get_path_list (registry));
137     }
138
139     dir_list = gst_registry_get_path_list(registry);
140     for(iter = dir_list; iter; iter = iter->next) {
141       dir = g_build_filename((const char *)iter->data, "register-scripts", NULL);
142       spawn_all_in_dir(dir);
143       g_free(dir);
144     }
145     g_list_free(dir_list);
146
147     registries = g_list_next (registries);
148   }
149
150   g_print ("loaded %d plugins with %d features\n", num_plugins, num_features);
151
152   return (0);
153 }
154