Audacious  $Id:Doxyfile42802007-03-2104:39:00Znenolod$
main.c
Go to the documentation of this file.
1 /*
2  * main.c
3  * Copyright 2007-2011 William Pitcock and John Lindgren
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions, and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions, and the following disclaimer in the documentation
13  * provided with the distribution.
14  *
15  * This software is provided "as is" and without any warranty, express or
16  * implied. In no event shall the authors be liable for any damages arising from
17  * the use of this software.
18  */
19 
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <locale.h>
27 
28 #include <gtk/gtk.h>
29 
30 #include <libaudcore/audstrings.h>
31 #include <libaudcore/hook.h>
32 #include <libaudtag/audtag.h>
33 
34 #ifdef USE_DBUS
35 #include "../libaudclient/audctrl.h"
36 #include "dbus.h"
37 #endif
38 
39 #include "debug.h"
40 #include "drct.h"
41 #include "equalizer.h"
42 #include "i18n.h"
43 #include "interface.h"
44 #include "main.h"
45 #include "misc.h"
46 #include "playlist.h"
47 #include "plugins.h"
48 #include "scanner.h"
49 #include "util.h"
50 
51 #define AUTOSAVE_INTERVAL 300 /* seconds */
52 
54 
55 static struct {
56  char **filenames;
57  int session;
65 } options;
66 
67 static char * aud_paths[AUD_PATH_COUNT];
68 
69 static void make_dirs(void)
70 {
71 #ifdef S_IRGRP
72  const mode_t mode755 = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
73 #else
74  const mode_t mode755 = S_IRWXU;
75 #endif
76 
79 }
80 
81 static void normalize_path (char * path)
82 {
83 #ifdef _WIN32
84  string_replace_char (path, '/', '\\');
85 #endif
86  int len = strlen (path);
87 #ifdef _WIN32
88  if (len > 3 && path[len - 1] == '\\') /* leave "C:\" */
89 #else
90  if (len > 1 && path[len - 1] == '/') /* leave leading "/" */
91 #endif
92  path[len - 1] = 0;
93 }
94 
95 static char * last_path_element (char * path)
96 {
97  char * slash = strrchr (path, G_DIR_SEPARATOR);
98  return (slash && slash[1]) ? slash + 1 : NULL;
99 }
100 
101 static void strip_path_element (char * path, char * elem)
102 {
103 #ifdef _WIN32
104  if (elem > path + 3)
105 #else
106  if (elem > path + 1)
107 #endif
108  elem[-1] = 0; /* overwrite slash */
109  else
110  elem[0] = 0; /* leave [drive letter and] leading slash */
111 }
112 
113 static void relocate_path (char * * pathp, const char * old, const char * new)
114 {
115  char * path = * pathp;
116  int oldlen = strlen (old);
117  int newlen = strlen (new);
118 
119  if (oldlen && old[oldlen - 1] == G_DIR_SEPARATOR)
120  oldlen --;
121  if (newlen && new[newlen - 1] == G_DIR_SEPARATOR)
122  newlen --;
123 
124 #ifdef _WIN32
125  if (strncasecmp (path, old, oldlen) || (path[oldlen] && path[oldlen] != G_DIR_SEPARATOR))
126 #else
127  if (strncmp (path, old, oldlen) || (path[oldlen] && path[oldlen] != G_DIR_SEPARATOR))
128 #endif
129  {
130  fprintf (stderr, "Failed to relocate a data path. Falling back to "
131  "compile-time path: %s\n", path);
132  return;
133  }
134 
135  * pathp = g_strdup_printf ("%.*s%s", newlen, new, path + oldlen);
136  g_free (path);
137 }
138 
139 static void relocate_paths (void)
140 {
141  /* Start with the paths hard coded at compile time. */
142  aud_paths[AUD_PATH_BIN_DIR] = g_strdup (HARDCODE_BINDIR);
143  aud_paths[AUD_PATH_DATA_DIR] = g_strdup (HARDCODE_DATADIR);
144  aud_paths[AUD_PATH_PLUGIN_DIR] = g_strdup (HARDCODE_PLUGINDIR);
145  aud_paths[AUD_PATH_LOCALE_DIR] = g_strdup (HARDCODE_LOCALEDIR);
146  aud_paths[AUD_PATH_DESKTOP_FILE] = g_strdup (HARDCODE_DESKTOPFILE);
147  aud_paths[AUD_PATH_ICON_FILE] = g_strdup (HARDCODE_ICONFILE);
154 
155  /* Compare the compile-time path to the executable and the actual path to
156  * see if we have been moved. */
157  char * old = g_strdup (aud_paths[AUD_PATH_BIN_DIR]);
158  char * new = get_path_to_self ();
159  if (! new)
160  {
161 ERR:
162  g_free (old);
163  g_free (new);
164  return;
165  }
166  normalize_path (new);
167 
168  /* Strip the name of the executable file, leaving the path. */
169  char * base = last_path_element (new);
170  if (! base)
171  goto ERR;
172  strip_path_element (new, base);
173 
174  /* Strip innermost folder names from both paths as long as they match. This
175  * leaves a compile-time prefix and a run-time one to replace it with. */
176  char * a, * b;
177  while ((a = last_path_element (old)) && (b = last_path_element (new)) &&
178 #ifdef _WIN32
179  ! strcasecmp (a, b))
180 #else
181  ! strcmp (a, b))
182 #endif
183  {
184  strip_path_element (old, a);
185  strip_path_element (new, b);
186  }
187 
188  /* Do the replacements. */
189  relocate_path (& aud_paths[AUD_PATH_BIN_DIR], old, new);
190  relocate_path (& aud_paths[AUD_PATH_DATA_DIR], old, new);
191  relocate_path (& aud_paths[AUD_PATH_PLUGIN_DIR], old, new);
192  relocate_path (& aud_paths[AUD_PATH_LOCALE_DIR], old, new);
193  relocate_path (& aud_paths[AUD_PATH_DESKTOP_FILE], old, new);
194  relocate_path (& aud_paths[AUD_PATH_ICON_FILE], old, new);
195 
196  g_free (old);
197  g_free (new);
198 }
199 
200 static void init_paths (void)
201 {
202  relocate_paths ();
203 
204  const char * xdg_config_home = g_get_user_config_dir ();
205  const char * xdg_data_home = g_get_user_data_dir ();
206 
207 #ifdef _WIN32
208  /* Some libraries (libmcs) and plugins (filewriter) use these variables,
209  * which are generally not set on Windows. */
210  g_setenv ("HOME", g_get_home_dir (), TRUE);
211  g_setenv ("XDG_CONFIG_HOME", xdg_config_home, TRUE);
212  g_setenv ("XDG_DATA_HOME", xdg_data_home, TRUE);
213  g_setenv ("XDG_CACHE_HOME", g_get_user_cache_dir (), TRUE);
214 #endif
215 
216  aud_paths[AUD_PATH_USER_DIR] = g_build_filename(xdg_config_home, "audacious", NULL);
217  aud_paths[AUD_PATH_USER_PLUGIN_DIR] = g_build_filename(xdg_data_home, "audacious", "Plugins", NULL);
218  aud_paths[AUD_PATH_PLAYLISTS_DIR] = g_build_filename(aud_paths[AUD_PATH_USER_DIR], "playlists", NULL);
219  aud_paths[AUD_PATH_GTKRC_FILE] = g_build_filename(aud_paths[AUD_PATH_USER_DIR], "gtkrc", NULL);
220 
221  for (int i = 0; i < AUD_PATH_COUNT; i ++)
222  AUDDBG ("Data path: %s\n", aud_paths[i]);
223 }
224 
225 const char * get_path (int id)
226 {
227  g_return_val_if_fail (id >= 0 && id < AUD_PATH_COUNT, NULL);
228  return aud_paths[id];
229 }
230 
231 static GOptionEntry cmd_entries[] = {
232  {"rew", 'r', 0, G_OPTION_ARG_NONE, &options.rew, N_("Skip backwards in playlist"), NULL},
233  {"play", 'p', 0, G_OPTION_ARG_NONE, &options.play, N_("Start playing current playlist"), NULL},
234  {"pause", 'u', 0, G_OPTION_ARG_NONE, &options.pause, N_("Pause current song"), NULL},
235  {"stop", 's', 0, G_OPTION_ARG_NONE, &options.stop, N_("Stop current song"), NULL},
236  {"play-pause", 't', 0, G_OPTION_ARG_NONE, &options.play_pause, N_("Pause if playing, play otherwise"), NULL},
237  {"fwd", 'f', 0, G_OPTION_ARG_NONE, &options.fwd, N_("Skip forward in playlist"), NULL},
238  {"show-jump-box", 'j', 0, G_OPTION_ARG_NONE, &options.show_jump_box, N_("Display Jump to File dialog"), NULL},
239  {"enqueue", 'e', 0, G_OPTION_ARG_NONE, &options.enqueue, N_("Add files to the playlist"), NULL},
240  {"enqueue-to-temp", 'E', 0, G_OPTION_ARG_NONE, &options.enqueue_to_temp, N_("Add new files to a temporary playlist"), NULL},
241  {"show-main-window", 'm', 0, G_OPTION_ARG_NONE, &options.mainwin, N_("Display the main window"), NULL},
242  {"headless", 'h', 0, G_OPTION_ARG_NONE, & headless, N_("Headless mode"), NULL},
243  {"quit-after-play", 'q', 0, G_OPTION_ARG_NONE, &options.quit_after_play, N_("Quit on playback stop"), NULL},
244  {"version", 'v', 0, G_OPTION_ARG_NONE, &options.version, N_("Show version"), NULL},
245  {"verbose", 'V', 0, G_OPTION_ARG_NONE, &options.verbose, N_("Print debugging messages"), NULL},
246  {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &options.filenames, N_("FILE..."), NULL},
247  {NULL},
248 };
249 
250 static void parse_options (int * argc, char *** argv)
251 {
252  GOptionContext *context;
253  GError *error = NULL;
254 
255  memset (& options, 0, sizeof options);
256  options.session = -1;
257 
258  context = g_option_context_new(_("- play multimedia files"));
259  g_option_context_add_main_entries(context, cmd_entries, PACKAGE);
260  g_option_context_add_group(context, gtk_get_option_group(FALSE));
261 
262  if (!g_option_context_parse(context, argc, argv, &error))
263  {
264  fprintf (stderr,
265  _("%s: %s\nTry `%s --help' for more information.\n"), (* argv)[0],
266  error->message, (* argv)[0]);
267  exit (EXIT_FAILURE);
268  }
269 
270  g_option_context_free (context);
271 
272  verbose = options.verbose;
273 }
274 
275 static bool_t get_lock (void)
276 {
277  char * path = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "lock", aud_paths[AUD_PATH_USER_DIR]);
278  int handle = open (path, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
279 
280  if (handle < 0)
281  {
282  if (errno != EEXIST)
283  fprintf (stderr, "Cannot create %s: %s.\n", path, strerror (errno));
284 
285  g_free (path);
286  return FALSE;
287  }
288 
289  close (handle);
290  g_free (path);
291  return TRUE;
292 }
293 
294 static void release_lock (void)
295 {
296  char * path = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "lock", aud_paths[AUD_PATH_USER_DIR]);
297  unlink (path);
298  g_free (path);
299 }
300 
301 static Index * convert_filenames (void)
302 {
303  if (! options.filenames)
304  return NULL;
305 
306  Index * filenames = index_new ();
307  char * * f = options.filenames;
308  char * cur = g_get_current_dir ();
309 
310  for (int i = 0; f[i]; i ++)
311  {
312  char * uri = NULL;
313 
314  if (strstr (f[i], "://"))
315  uri = str_get (f[i]);
316  else if (g_path_is_absolute (f[i]))
317  {
318  char * tmp = filename_to_uri (f[i]);
319  uri = str_get (tmp);
320  free (tmp);
321  }
322  else
323  {
324  char * tmp = g_build_filename (cur, f[i], NULL);
325  char * tmp2 = filename_to_uri (tmp);
326  uri = str_get (tmp2);
327  free (tmp);
328  free (tmp2);
329  }
330 
331  if (uri)
332  index_append (filenames, uri);
333  }
334 
335  g_free (cur);
336  return filenames;
337 }
338 
339 static void do_remote (void)
340 {
341 #ifdef USE_DBUS
342  DBusGProxy * session = audacious_get_dbus_proxy ();
343 
344  if (session && audacious_remote_is_running (session))
345  {
346  Index * filenames = convert_filenames ();
347 
348  /* if no command line options, then present running instance */
349  if (! (filenames || options.play || options.pause || options.play_pause ||
350  options.stop || options.rew || options.fwd || options.show_jump_box ||
351  options.mainwin))
352  options.mainwin = TRUE;
353 
354  if (filenames)
355  {
356  GList * list = NULL;
357 
358  for (int f = index_count (filenames); f --; )
359  list = g_list_prepend (list, index_get (filenames, f));
360 
361  if (options.enqueue_to_temp)
363  else if (options.enqueue)
364  audacious_remote_playlist_add (session, list);
365  else
366  audacious_remote_playlist_open_list (session, list);
367 
368  g_list_free (list);
369 
370  for (int f = 0; f < index_count (filenames); f ++)
371  str_unref (index_get (filenames, f));
372 
373  index_free (filenames);
374  }
375 
376  if (options.play)
377  audacious_remote_play (session);
378  if (options.pause)
379  audacious_remote_pause (session);
380  if (options.play_pause)
381  audacious_remote_play_pause (session);
382  if (options.stop)
383  audacious_remote_stop (session);
384  if (options.rew)
386  if (options.fwd)
388  if (options.show_jump_box)
390  if (options.mainwin)
392 
393  exit (EXIT_SUCCESS);
394  }
395 #endif
396 
397  fprintf (stderr, "WARNING: Audacious seems to be already running but is not responding.\n");
398 }
399 
400 static void do_commands (void)
401 {
402  bool_t resume = get_bool (NULL, "resume_playback_on_startup");
403 
404  Index * filenames = convert_filenames ();
405  if (filenames)
406  {
407  if (options.enqueue_to_temp)
408  {
409  drct_pl_open_temp_list (filenames);
410  resume = FALSE;
411  }
412  else if (options.enqueue)
413  drct_pl_add_list (filenames, -1);
414  else
415  {
416  drct_pl_open_list (filenames);
417  resume = FALSE;
418  }
419  }
420 
421  if (resume)
422  playlist_resume ();
423 
424  if (options.play || options.play_pause)
425  {
426  if (! drct_get_playing ())
427  drct_play ();
428  else if (drct_get_paused ())
429  drct_pause ();
430  }
431 
432  if (options.show_jump_box)
434  if (options.mainwin)
436 }
437 
438 static void init_one (void)
439 {
440  init_paths ();
441  make_dirs ();
442 
443  setlocale (LC_ALL, "");
444  bindtextdomain (PACKAGE, aud_paths[AUD_PATH_LOCALE_DIR]);
445  bind_textdomain_codeset (PACKAGE, "UTF-8");
446  bindtextdomain (PACKAGE "-plugins", aud_paths[AUD_PATH_LOCALE_DIR]);
447  bind_textdomain_codeset (PACKAGE "-plugins", "UTF-8");
448  textdomain (PACKAGE);
449 }
450 
451 static void init_two (int * p_argc, char * * * p_argv)
452 {
453  if (! headless)
454  {
455 #if ! GLIB_CHECK_VERSION (2, 32, 0)
456  g_thread_init (NULL);
457 #endif
458  gtk_init (p_argc, p_argv);
459  }
460 
461  AUDDBG ("Loading configuration.\n");
462  config_load ();
463 
464  AUDDBG ("Initializing.\n");
465  art_init ();
466  chardet_init ();
467  eq_init ();
468  playlist_init ();
469 
470 #ifdef HAVE_SIGWAIT
471  signals_init ();
472 #endif
473 
474  tag_set_verbose (verbose);
476 
477  AUDDBG ("Loading lowlevel plugins.\n");
479 
480  AUDDBG ("Starting worker threads.\n");
481  adder_init ();
482  scanner_init ();
483 
484  AUDDBG ("Restoring state.\n");
485  load_playlists ();
486 
487  do_commands ();
488 
489  AUDDBG ("Loading highlevel plugins.\n");
491 
492 #ifdef USE_DBUS
493  init_dbus ();
494 #endif
495 
497 }
498 
499 static void shut_down (void)
500 {
502 
503  AUDDBG ("Saving playlist state.\n");
505 
506  AUDDBG ("Unloading highlevel plugins.\n");
507  stop_plugins_two ();
508 
509 #ifdef USE_DBUS
510  cleanup_dbus ();
511 #endif
512 
513  AUDDBG ("Stopping playback.\n");
514  if (drct_get_playing ())
515  drct_stop ();
516 
517  AUDDBG ("Stopping worker threads.\n");
518  adder_cleanup ();
519  scanner_cleanup ();
520 
521  AUDDBG ("Unloading lowlevel plugins.\n");
522  stop_plugins_one ();
523 
524  AUDDBG ("Saving configuration.\n");
525  config_save ();
526  config_cleanup ();
527 
528  AUDDBG ("Cleaning up.\n");
529  art_cleanup ();
530  eq_cleanup ();
531  history_cleanup ();
532  playlist_end ();
533 
534  strpool_shutdown ();
535 }
536 
538 {
539  AUDDBG ("Saving configuration.\n");
540  hook_call ("config save", NULL);
542  config_save ();
543  return TRUE;
544 }
545 
547 {
548  return options.quit_after_play && ! drct_get_playing () && ! playlist_add_in_progress (-1);
549 }
550 
551 static void maybe_quit (void)
552 {
553  if (check_should_quit ())
554  gtk_main_quit ();
555 }
556 
557 int main(int argc, char ** argv)
558 {
559  init_one ();
560  parse_options (& argc, & argv);
561 
562  if (options.version)
563  {
564  printf ("%s %s (%s)\n", _("Audacious"), VERSION, BUILDSTAMP);
565  return EXIT_SUCCESS;
566  }
567 
568  if (! get_lock ())
569  do_remote (); /* may exit */
570 
571  AUDDBG ("No remote session; starting up.\n");
572  init_two (& argc, & argv);
573 
574  AUDDBG ("Startup complete.\n");
575  g_timeout_add_seconds (AUTOSAVE_INTERVAL, (GSourceFunc) do_autosave, NULL);
576 
577  if (check_should_quit ())
578  goto QUIT;
579 
580  hook_associate ("playback stop", (HookFunction) maybe_quit, NULL);
581  hook_associate ("playlist add complete", (HookFunction) maybe_quit, NULL);
582  hook_associate ("quit", (HookFunction) gtk_main_quit, NULL);
583 
584  gtk_main ();
585 
586  hook_dissociate ("playback stop", (HookFunction) maybe_quit);
587  hook_dissociate ("playlist add complete", (HookFunction) maybe_quit);
588  hook_dissociate ("quit", (HookFunction) gtk_main_quit);
589 
590 QUIT:
591  shut_down ();
592  release_lock ();
593  return EXIT_SUCCESS;
594 }