00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <glib.h>
00027
00028
00029 #if !GLIB_CHECK_VERSION(2,8,0)
00030 # error You need atleast glib 2.8.0
00031 #endif
00032
00033 #include "xmms_configuration.h"
00034 #include "xmmsc/xmmsc_util.h"
00035 #include "xmmspriv/xmms_plugin.h"
00036 #include "xmmspriv/xmms_config.h"
00037 #include "xmmspriv/xmms_playlist.h"
00038 #include "xmmspriv/xmms_collection.h"
00039 #include "xmmspriv/xmms_signal.h"
00040 #include "xmmspriv/xmms_symlink.h"
00041 #include "xmmspriv/xmms_checkroot.h"
00042 #include "xmmspriv/xmms_medialib.h"
00043 #include "xmmspriv/xmms_output.h"
00044 #include "xmmspriv/xmms_ipc.h"
00045 #include "xmmspriv/xmms_log.h"
00046 #include "xmmspriv/xmms_sqlite.h"
00047 #include "xmmspriv/xmms_xform.h"
00048 #include "xmmspriv/xmms_bindata.h"
00049 #include "xmmspriv/xmms_utils.h"
00050
00051 #include <stdio.h>
00052 #include <stdlib.h>
00053 #include <string.h>
00054 #include <unistd.h>
00055 #include <signal.h>
00056 #include <sys/stat.h>
00057 #include <fcntl.h>
00058
00059
00060
00061
00062 static void quit (xmms_object_t *object, xmms_error_t *error);
00063 static GTree *stats (xmms_object_t *object, xmms_error_t *error);
00064 static void hello (xmms_object_t *object, guint protocolver, gchar *client, xmms_error_t *error);
00065 static void install_scripts (const gchar *into_dir);
00066 static xmms_xform_object_t *xform_obj;
00067 static xmms_bindata_t *bindata_obj;
00068
00069 XMMS_CMD_DEFINE (quit, quit, xmms_object_t*, NONE, NONE, NONE);
00070 XMMS_CMD_DEFINE (hello, hello, xmms_object_t *, NONE, UINT32, STRING);
00071 XMMS_CMD_DEFINE (stats, stats, xmms_object_t *, DICT, NONE, NONE);
00072 XMMS_CMD_DEFINE (plugin_list, xmms_plugin_client_list, xmms_object_t *, LIST, UINT32, NONE);
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 struct xmms_main_St {
00092 xmms_object_t object;
00093 xmms_output_t *output;
00094 time_t starttime;
00095 };
00096
00097 typedef struct xmms_main_St xmms_main_t;
00098
00099
00100 static GMainLoop *mainloop;
00101
00102
00103 static gchar *conffile = NULL;
00104
00105
00106
00107
00108 static GTree *
00109 stats (xmms_object_t *object, xmms_error_t *error)
00110 {
00111 GTree *ret;
00112 gint starttime;
00113
00114 ret = g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
00115 NULL, (GDestroyNotify)xmms_object_cmd_value_unref);
00116
00117 starttime = ((xmms_main_t*)object)->starttime;
00118
00119 g_tree_insert (ret, (gpointer) "version",
00120 xmms_object_cmd_value_str_new (XMMS_VERSION));
00121 g_tree_insert (ret, (gpointer) "uptime",
00122 xmms_object_cmd_value_int_new (time (NULL)-starttime));
00123
00124 return ret;
00125 }
00126
00127
00128
00129
00130
00131
00132
00133
00134 static void
00135 do_scriptdir (const gchar *scriptdir)
00136 {
00137 GError *err = NULL;
00138 GDir *dir;
00139 const gchar *f;
00140 gchar *argv[2] = {NULL, NULL};
00141
00142 XMMS_DBG ("Running scripts in %s", scriptdir);
00143 if (!g_file_test (scriptdir, G_FILE_TEST_IS_DIR)) {
00144 g_mkdir_with_parents (scriptdir, 0755);
00145 install_scripts (scriptdir);
00146 }
00147
00148 dir = g_dir_open (scriptdir, 0, &err);
00149 if (!dir) {
00150 xmms_log_error ("Could not open script dir '%s' error: %s", scriptdir, err->message);
00151 return;
00152 }
00153
00154 while ((f = g_dir_read_name (dir))) {
00155 argv[0] = g_strdup_printf ("%s/%s", scriptdir, f);
00156 if (g_file_test (argv[0], G_FILE_TEST_IS_EXECUTABLE)) {
00157 if (!g_spawn_async (g_get_home_dir (),
00158 argv, NULL, 0, NULL, NULL, NULL, &err)) {
00159 xmms_log_error ("Could not run script '%s', error: %s",
00160 argv[0], err->message);
00161 }
00162 }
00163 g_free (argv[0]);
00164 }
00165
00166 g_dir_close (dir);
00167
00168 }
00169
00170
00171
00172
00173
00174 static void
00175 load_config ()
00176 {
00177 gchar configdir[PATH_MAX];
00178
00179 if (!conffile) {
00180 conffile = XMMS_BUILD_PATH ("xmms2.conf");
00181 }
00182
00183 g_assert (strlen (conffile) <= XMMS_MAX_CONFIGFILE_LEN);
00184
00185 if (!xmms_userconfdir_get (configdir, sizeof (configdir))) {
00186 xmms_log_error ("Could not get path to config dir");
00187 } else if (!g_file_test (configdir, G_FILE_TEST_IS_DIR)) {
00188 g_mkdir_with_parents (configdir, 0755);
00189 }
00190
00191 xmms_config_init (conffile);
00192 }
00193
00194
00195
00196
00197
00198
00199
00200 static void
00201 change_output (xmms_object_t *object, gconstpointer data, gpointer userdata)
00202 {
00203 xmms_output_plugin_t *plugin;
00204 xmms_main_t *mainobj = (xmms_main_t*)userdata;
00205 gchar *outname = (gchar *) data;
00206
00207 if (!mainobj->output)
00208 return;
00209
00210 xmms_log_info ("Switching to output %s", outname);
00211
00212 plugin = (xmms_output_plugin_t *)xmms_plugin_find (XMMS_PLUGIN_TYPE_OUTPUT, outname);
00213 if (!plugin) {
00214 xmms_log_error ("Baaaaad output plugin, try to change the output.plugin config variable to something usefull");
00215 } else {
00216 if (!xmms_output_plugin_switch (mainobj->output, plugin)) {
00217 xmms_log_error ("Baaaaad output plugin, try to change the output.plugin config variable to something usefull");
00218 }
00219 }
00220 }
00221
00222
00223
00224
00225
00226 static void
00227 xmms_main_destroy (xmms_object_t *object)
00228 {
00229 xmms_main_t *mainobj = (xmms_main_t *) object;
00230 xmms_object_cmd_arg_t arg;
00231 xmms_config_property_t *cv;
00232
00233 cv = xmms_config_lookup ("core.shutdownpath");
00234 do_scriptdir (xmms_config_property_get_string (cv));
00235
00236
00237 xmms_object_cmd_arg_init (&arg);
00238
00239 xmms_object_cmd_call (XMMS_OBJECT (mainobj->output),
00240 XMMS_IPC_CMD_STOP, &arg);
00241
00242 g_usleep (G_USEC_PER_SEC);
00243 xmms_object_unref (mainobj->output);
00244
00245 xmms_object_unref (xform_obj);
00246
00247 xmms_config_save ();
00248
00249 xmms_config_shutdown ();
00250 xmms_plugin_shutdown ();
00251
00252 xmms_ipc_object_unregister (XMMS_IPC_OBJECT_MAIN);
00253 xmms_ipc_shutdown ();
00254
00255 xmms_log_shutdown ();
00256 }
00257
00258
00259
00260
00261 static void
00262 hello (xmms_object_t *object, guint protocolver, gchar *client, xmms_error_t *error)
00263 {
00264 if (protocolver != XMMS_IPC_PROTOCOL_VERSION) {
00265 xmms_log_info ("Client '%s' with bad protocol version (%d, not %d) connected", client, protocolver, XMMS_IPC_PROTOCOL_VERSION);
00266 xmms_error_set (error, XMMS_ERROR_INVAL, "Bad protocol version");
00267 return;
00268 }
00269 XMMS_DBG ("Client '%s' connected", client);
00270 }
00271
00272 static gboolean
00273 kill_server (gpointer object) {
00274 xmms_object_emit_f (XMMS_OBJECT (object),
00275 XMMS_IPC_SIGNAL_QUIT,
00276 XMMS_OBJECT_CMD_ARG_UINT32,
00277 time (NULL)-((xmms_main_t*)object)->starttime);
00278
00279 xmms_object_unref (object);
00280
00281 exit (EXIT_SUCCESS);
00282 }
00283
00284
00285
00286
00287
00288 static void
00289 quit (xmms_object_t *object, xmms_error_t *error)
00290 {
00291
00292
00293
00294
00295
00296 g_timeout_add (1, kill_server, object);
00297 }
00298
00299 static void
00300 install_scripts (const gchar *into_dir)
00301 {
00302 GDir *dir;
00303 GError *err = NULL;
00304 gchar path[PATH_MAX];
00305 const gchar *f;
00306 gchar *s;
00307
00308 s = strrchr (into_dir, G_DIR_SEPARATOR);
00309 if (!s)
00310 return;
00311
00312 s++;
00313
00314 g_snprintf (path, PATH_MAX, "%s/scripts/%s", SHAREDDIR, s);
00315 xmms_log_info ("Installing scripts from %s", path);
00316 dir = g_dir_open (path, 0, &err);
00317 if (!dir) {
00318 xmms_log_error ("Global script directory not found");
00319 return;
00320 }
00321
00322 while ((f = g_dir_read_name (dir))) {
00323 gchar *source = g_strdup_printf ("%s/%s", path, f);
00324 gchar *dest = g_strdup_printf ("%s/%s", into_dir, f);
00325 if (!xmms_symlink_file (source, dest)) {
00326 break;
00327 }
00328 g_free (source);
00329 g_free (dest);
00330 }
00331
00332 g_dir_close (dir);
00333 }
00334
00335
00336
00337
00338 void
00339 print_version ()
00340 {
00341 printf ("XMMS2 version " XMMS_VERSION "\n");
00342 printf ("Copyright (C) 2003-2008 XMMS2 Team\n");
00343 printf ("This is free software; see the source for copying conditions.\n");
00344 printf ("There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n");
00345 printf ("PARTICULAR PURPOSE.\n");
00346 printf (" Using glib version %d.%d.%d (compiled against "
00347 G_STRINGIFY (GLIB_MAJOR_VERSION) "."
00348 G_STRINGIFY (GLIB_MINOR_VERSION) "."
00349 G_STRINGIFY (GLIB_MICRO_VERSION) ")\n",
00350 glib_major_version,
00351 glib_minor_version,
00352 glib_micro_version);
00353 xmms_sqlite_print_version ();
00354
00355 exit (EXIT_SUCCESS);
00356 }
00357
00358
00359
00360
00361 int
00362 main (int argc, char **argv)
00363 {
00364 xmms_output_plugin_t *o_plugin;
00365 xmms_config_property_t *cv;
00366 xmms_main_t *mainobj;
00367 int loglevel = 1;
00368 xmms_playlist_t *playlist;
00369 gchar default_path[XMMS_PATH_MAX + 16], *tmp;
00370 gboolean verbose = FALSE;
00371 gboolean quiet = FALSE;
00372 gboolean version = FALSE;
00373 gboolean nologging = FALSE;
00374 gboolean runasroot = FALSE;
00375 gboolean showhelp = FALSE;
00376 const gchar *outname = NULL;
00377 const gchar *ipcpath = NULL;
00378 gchar *ppath = NULL;
00379 int status_fd = -1;
00380 GOptionContext *context = NULL;
00381 GError *error = NULL;
00382
00383
00384
00385
00386 GOptionEntry opts[] = {
00387 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Increase verbosity", NULL},
00388 {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet, "Decrease verbosity", NULL},
00389 {"version", 'V', 0, G_OPTION_ARG_NONE, &version, "Print version", NULL},
00390 {"no-logging", 'n', 0, G_OPTION_ARG_NONE, &nologging, "Disable logging", NULL},
00391 {"output", 'o', 0, G_OPTION_ARG_STRING, &outname, "Use 'x' as output plugin", "<x>"},
00392 {"ipc-socket", 'i', 0, G_OPTION_ARG_FILENAME, &ipcpath, "Listen to socket 'url'", "<url>"},
00393 {"plugindir", 'p', 0, G_OPTION_ARG_FILENAME, &ppath, "Search for plugins in directory 'foo'", "<foo>"},
00394 {"conf", 'c', 0, G_OPTION_ARG_FILENAME, &conffile, "Specify alternate configuration file", "<file>"},
00395 {"status-fd", 's', 0, G_OPTION_ARG_INT, &status_fd, "Specify a filedescriptor to write to when started", "fd"},
00396 {"yes-run-as-root", 0, 0, G_OPTION_ARG_NONE, &runasroot, "Give me enough rope to shoot myself in the foot", NULL},
00397 {"show-help", 'h', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &showhelp, "Use --help or -? instead", NULL},
00398 {NULL}
00399 };
00400
00401
00402 if (glib_major_version != GLIB_MAJOR_VERSION ||
00403 glib_minor_version < GLIB_MINOR_VERSION) {
00404 g_print ("xmms2d is build against version %d.%d,\n"
00405 "but is (runtime) linked against %d.%d.\n"
00406 "Refusing to start.\n",
00407 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION,
00408 glib_major_version, glib_minor_version);
00409 exit (EXIT_FAILURE);
00410 }
00411
00412 xmms_signal_block ();
00413
00414 context = g_option_context_new ("- XMMS2 Daemon");
00415 g_option_context_add_main_entries (context, opts, NULL);
00416 if (!g_option_context_parse (context, &argc, &argv, &error) || error) {
00417 g_print ("Error parsing options: %s\n", error->message);
00418 g_clear_error (&error);
00419 exit (EXIT_FAILURE);
00420 }
00421 if (showhelp) {
00422 #if GLIB_CHECK_VERSION(2,14,0)
00423 g_print (g_option_context_get_help (context, TRUE, NULL));
00424 exit (EXIT_SUCCESS);
00425 #else
00426 g_print ("Please use --help or -? for help\n");
00427 exit (EXIT_FAILURE);
00428 #endif
00429 }
00430 g_option_context_free (context);
00431
00432 if (argc != 1) {
00433 g_print ("There were unknown options, aborting!\n");
00434 exit (EXIT_FAILURE);
00435 }
00436
00437 if (xmms_checkroot ()) {
00438 if (runasroot) {
00439 g_print ("***************************************\n");
00440 g_print ("Warning! You are running XMMS2D as root, this is a bad idea!\nBut I'll allow it since you asked nicely.\n");
00441 g_print ("***************************************\n\n");
00442 } else {
00443 g_print ("PLEASE DON'T RUN XMMS2D AS ROOT!\n\n(if you really must, read the help)\n");
00444 exit (EXIT_FAILURE);
00445 }
00446 }
00447
00448 if (verbose) {
00449 loglevel++;
00450 } else if (quiet) {
00451 loglevel--;
00452 }
00453
00454 if (version) {
00455 print_version ();
00456 }
00457
00458 g_thread_init (NULL);
00459
00460 g_random_set_seed (time (NULL));
00461
00462 xmms_log_init (loglevel);
00463 xmms_ipc_init ();
00464
00465 load_config ();
00466
00467 xmms_fallback_ipcpath_get (default_path, sizeof (default_path));
00468
00469 cv = xmms_config_property_register ("core.ipcsocket",
00470 default_path,
00471 on_config_ipcsocket_change,
00472 NULL);
00473
00474 if (!ipcpath) {
00475
00476
00477
00478
00479 ipcpath = xmms_config_property_get_string (cv);
00480 }
00481
00482 if (!xmms_ipc_setup_server (ipcpath)) {
00483 xmms_ipc_shutdown ();
00484 xmms_log_fatal ("IPC failed to init!");
00485 }
00486
00487 if (!xmms_plugin_init (ppath)) {
00488 return 1;
00489 }
00490
00491 playlist = xmms_playlist_init ();
00492 xform_obj = xmms_xform_object_init ();
00493 bindata_obj = xmms_bindata_init ();
00494
00495 mainobj = xmms_object_new (xmms_main_t, xmms_main_destroy);
00496
00497
00498 cv = xmms_config_property_register ("output.plugin",
00499 XMMS_OUTPUT_DEFAULT,
00500 change_output, mainobj);
00501
00502 if (outname) {
00503 xmms_config_setvalue (NULL, "output.plugin", outname, NULL);
00504 }
00505
00506 outname = xmms_config_property_get_string (cv);
00507 xmms_log_info ("Using output plugin: %s", outname);
00508 o_plugin = (xmms_output_plugin_t *)xmms_plugin_find (XMMS_PLUGIN_TYPE_OUTPUT, outname);
00509 if (!o_plugin) {
00510 xmms_log_error ("Baaaaad output plugin, try to change the"
00511 "output.plugin config variable to something usefull");
00512 }
00513
00514 mainobj->output = xmms_output_new (o_plugin, playlist);
00515 if (!mainobj->output) {
00516 xmms_log_fatal ("Failed to create output object!");
00517 }
00518
00519 if (status_fd != -1) {
00520 write (status_fd, "+", 1);
00521 }
00522
00523 xmms_signal_init (XMMS_OBJECT (mainobj));
00524
00525 xmms_ipc_object_register (XMMS_IPC_OBJECT_MAIN,
00526 XMMS_OBJECT (mainobj));
00527
00528 xmms_ipc_broadcast_register (XMMS_OBJECT (mainobj),
00529 XMMS_IPC_SIGNAL_QUIT);
00530
00531 xmms_object_cmd_add (XMMS_OBJECT (mainobj),
00532 XMMS_IPC_CMD_QUIT,
00533 XMMS_CMD_FUNC (quit));
00534 xmms_object_cmd_add (XMMS_OBJECT (mainobj),
00535 XMMS_IPC_CMD_HELLO,
00536 XMMS_CMD_FUNC (hello));
00537 xmms_object_cmd_add (XMMS_OBJECT (mainobj),
00538 XMMS_IPC_CMD_PLUGIN_LIST,
00539 XMMS_CMD_FUNC (plugin_list));
00540 xmms_object_cmd_add (XMMS_OBJECT (mainobj),
00541 XMMS_IPC_CMD_STATS,
00542 XMMS_CMD_FUNC (stats));
00543
00544
00545 mainobj->starttime = time (NULL);
00546
00547
00548 g_strlcpy (default_path, ipcpath, sizeof (default_path));
00549
00550 tmp = strchr (default_path, ';');
00551 if (tmp) {
00552 *tmp = '\0';
00553 }
00554
00555 putenv (g_strdup_printf ("XMMS_PATH=%s", default_path));
00556
00557
00558 putenv (g_strdup_printf ("XMMS_PATH_FULL=%s", ipcpath));
00559
00560 tmp = XMMS_BUILD_PATH ("shutdown.d");
00561 cv = xmms_config_property_register ("core.shutdownpath",
00562 tmp, NULL, NULL);
00563 g_free (tmp);
00564
00565 tmp = XMMS_BUILD_PATH ("startup.d");
00566 cv = xmms_config_property_register ("core.startuppath",
00567 tmp, NULL, NULL);
00568 g_free (tmp);
00569
00570
00571 do_scriptdir (xmms_config_property_get_string (cv));
00572
00573 mainloop = g_main_loop_new (NULL, FALSE);
00574
00575 g_main_loop_run (mainloop);
00576
00577 return 0;
00578 }
00579
00580