<commit_before>// Licensed GNU LGPL v2.1 or later: http://www.gnu.org/licenses/lgpl.html
#include "bstutils.hh"
#include "bstapp.hh"
#include "bstsplash.hh"
#include "bstxkb.hh"
#include "bstgconfig.hh"
#include "bstkeybindings.hh"
#include "bstskinconfig.hh"
#include "bstmsgabsorb.hh"
#include "bstusermessage.hh"
#include "bstparam.hh"
#include "bstpreferences.hh"
#include "data/beast-images.h"
#include "../configure.h"
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <sys/resource.h>

extern "C" void bse_object_debug_leaks (void); // FIXME

/* --- prototypes --- */
static void			bst_args_parse_early	(int *argc_p, char **argv);
static void			bst_args_process        (int *argc_p, char **argv);
static void			bst_print_blurb		(void);
static void			bst_exit_print_version	(void);
static void                     bst_init_aida_idl       ();

/* --- variables --- */
gboolean            bst_developer_hints = FALSE;
gboolean            bst_debug_extensions = FALSE;
gboolean            bst_main_loop_running = TRUE;
static GtkWidget   *beast_splash = NULL;
static gboolean     registration_done = FALSE;
static gboolean     arg_force_xkb = FALSE;
static gboolean     register_core_plugins = TRUE;
static gboolean     register_ladspa_plugins = TRUE;
static gboolean     register_scripts = TRUE;
static gboolean     may_auto_update_bse_rc_file = TRUE;
static bool         rewrite_bse_file = false;

/* --- functions --- */
static void
server_registration (SfiProxy     server,
		     SfiChoice    rchoice,
		     const gchar *what,
		     const gchar *error,
		     gpointer     data)
{
  BseRegistrationType rtype = bse_registration_type_from_choice (rchoice);

  if (rtype == BSE_REGISTER_DONE)
    registration_done = TRUE;
  else
    {
      const char *base = strrchr (what, '/');
      bst_splash_update_item ((GtkWidget*) data, base ? base + 1 : what);
      if (error && error[0])
	g_message ("failed to register \"%s\": %s", what, error);
    }
}

static void     main_load_rc_files();
static void     main_show_splash_image();
static void     main_init_core_plugins();
static void     main_init_ladspa();
static void     main_sleep4gdb();
static void     main_init_scripts();
static void     main_init_dialogs();
static BstApp*  main_open_files (int filesc, char **filesv);
static BstApp*  main_open_default_window();
static void     main_show_release_notes();
static void     main_splash_down ();
static void     main_run_event_loops ();
static bool     force_saving_rc_files = false;
static void     main_save_rc_files ();
static void     main_cleanup ();

int
main (int argc, char *argv[])
{
  GSource *source;
  /* initialize i18n */
  bindtextdomain (BST_GETTEXT_DOMAIN, bse_installpath (BSE_INSTALLPATH_LOCALEBASE).c_str());
  bind_textdomain_codeset (BST_GETTEXT_DOMAIN, "UTF-8");
  textdomain (BST_GETTEXT_DOMAIN);
  setlocale (LC_ALL, "");
  /* initialize random numbers */
  struct timeval tv;
  gettimeofday (&tv, NULL);
  srand48 (tv.tv_usec + (tv.tv_sec << 16));
  srand (tv.tv_usec + (tv.tv_sec << 16));

  // initialize threading and GLib types
  Rapicorn::ThreadInfo::self().name ("Beast GUI");
  Bse::TaskRegistry::add (Rapicorn::ThreadInfo::self().name(), Rapicorn::ThisThread::process_pid(), Rapicorn::ThisThread::thread_pid());

  /* initialize Birnet/Sfi */
  sfi_init (&argc, argv, "BEAST");
  /* ensure SFI can wake us up */

  // early arg parsing without remote calls
  bst_args_parse_early (&argc, argv);

  // startup BSE, allow remote calls
  Bse::String bseoptions = Bse::string_format ("debug-extensions=%d", bst_debug_extensions);
  Bse::init_async (&argc, argv, "BEAST", Bse::string_split (bseoptions, ":")); // initializes Bse AIDA connection
  // now that the BSE thread runs, drop scheduling priorities if we have any
  setpriority (PRIO_PROCESS, getpid(), 0);
  // hook up Bse aida IDL with main loop
  bst_init_aida_idl();
  // Setup SFI glue context and wakeup
  sfi_glue_context_push (Bse::init_glue_context ("BEAST", bst_main_loop_wakeup));
  source = g_source_simple (GDK_PRIORITY_EVENTS, // G_PRIORITY_HIGH - 100,
			    (GSourcePending) sfi_glue_context_pending,
			    (GSourceDispatch) sfi_glue_context_dispatch,
			    NULL, NULL, NULL);
  g_source_attach (source, NULL);
  g_source_unref (source);

  // arg processing with BSE available, --help, --version
  bst_args_process (&argc, argv);

  // initialize Gtk+ and go into threading mode
  gtk_init (&argc, &argv);
  GDK_THREADS_ENTER ();
  /* initialize Gtk+ Extension Kit */
  gxk_init ();
  /* documentation search paths */
  gxk_text_add_tsm_path (bse_installpath (BSE_INSTALLPATH_DOCDIR).c_str());
  gxk_text_add_tsm_path (bse_installpath (BSE_INSTALLPATH_DATADIR_IMAGES).c_str());
  gxk_text_add_tsm_path (".");
  /* now, we can popup the splash screen */
  beast_splash = bst_splash_new ("BEAST-Splash", BST_SPLASH_WIDTH, BST_SPLASH_HEIGHT, 15);
  bst_splash_set_title (beast_splash, _("BEAST Startup"));
  gtk_object_set_user_data (GTK_OBJECT (beast_splash), NULL);	/* fix for broken user_data in 2.2 */
  bst_splash_set_text (beast_splash,
		       Rapicorn::string_format ("<b><big>BEAST</big></b>\n"
                                                "<b>The BSE Equipped Audio Synthesizer and Tracker</b>\n"
                                                "<b>Version %s (%s)</b>\n",
                                                BST_VERSION, BST_VERSION_HINT));
  bst_splash_update_entity (beast_splash, _("Startup"));
  bst_splash_show_grab (beast_splash);

  /* BEAST initialization */
  bst_splash_update_item (beast_splash, _("Initializers"));
  _bst_init_utils ();
  _bst_init_params ();
  _bst_gconfig_init ();
  _bst_skin_config_init ();
  _bst_msg_absorb_config_init ();

  main_load_rc_files();
  main_show_splash_image();
  main_init_core_plugins();
  main_init_ladspa();
  main_sleep4gdb();
  main_init_scripts();
  main_init_dialogs();
  BstApp *app = main_open_files (argc - 1, &argv[1]);
  if (!app)
    app = main_open_default_window();
  main_show_release_notes();
  main_splash_down();
  main_run_event_loops();
  main_save_rc_files();
  main_cleanup();

  return 0;
}

static void
main_load_rc_files()<commit_msg>BEAST: bstmain.cc: extract method main_init_bst_systems<commit_after>// Licensed GNU LGPL v2.1 or later: http://www.gnu.org/licenses/lgpl.html
#include "bstutils.hh"
#include "bstapp.hh"
#include "bstsplash.hh"
#include "bstxkb.hh"
#include "bstgconfig.hh"
#include "bstkeybindings.hh"
#include "bstskinconfig.hh"
#include "bstmsgabsorb.hh"
#include "bstusermessage.hh"
#include "bstparam.hh"
#include "bstpreferences.hh"
#include "data/beast-images.h"
#include "../configure.h"
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <sys/resource.h>

extern "C" void bse_object_debug_leaks (void); // FIXME

/* --- prototypes --- */
static void			bst_args_parse_early	(int *argc_p, char **argv);
static void			bst_args_process        (int *argc_p, char **argv);
static void			bst_print_blurb		(void);
static void			bst_exit_print_version	(void);
static void                     bst_init_aida_idl       ();

/* --- variables --- */
gboolean            bst_developer_hints = FALSE;
gboolean            bst_debug_extensions = FALSE;
gboolean            bst_main_loop_running = TRUE;
static GtkWidget   *beast_splash = NULL;
static gboolean     registration_done = FALSE;
static gboolean     arg_force_xkb = FALSE;
static gboolean     register_core_plugins = TRUE;
static gboolean     register_ladspa_plugins = TRUE;
static gboolean     register_scripts = TRUE;
static gboolean     may_auto_update_bse_rc_file = TRUE;
static bool         rewrite_bse_file = false;

/* --- functions --- */
static void
server_registration (SfiProxy     server,
		     SfiChoice    rchoice,
		     const gchar *what,
		     const gchar *error,
		     gpointer     data)
{
  BseRegistrationType rtype = bse_registration_type_from_choice (rchoice);

  if (rtype == BSE_REGISTER_DONE)
    registration_done = TRUE;
  else
    {
      const char *base = strrchr (what, '/');
      bst_splash_update_item ((GtkWidget*) data, base ? base + 1 : what);
      if (error && error[0])
	g_message ("failed to register \"%s\": %s", what, error);
    }
}

static void     main_init_bst_systems();
static void     main_load_rc_files();
static void     main_show_splash_image();
static void     main_init_core_plugins();
static void     main_init_ladspa();
static void     main_sleep4gdb();
static void     main_init_scripts();
static void     main_init_dialogs();
static BstApp*  main_open_files (int filesc, char **filesv);
static BstApp*  main_open_default_window();
static void     main_show_release_notes();
static void     main_splash_down ();
static void     main_run_event_loops ();
static bool     force_saving_rc_files = false;
static void     main_save_rc_files ();
static void     main_cleanup ();

int
main (int argc, char *argv[])
{
  GSource *source;
  /* initialize i18n */
  bindtextdomain (BST_GETTEXT_DOMAIN, bse_installpath (BSE_INSTALLPATH_LOCALEBASE).c_str());
  bind_textdomain_codeset (BST_GETTEXT_DOMAIN, "UTF-8");
  textdomain (BST_GETTEXT_DOMAIN);
  setlocale (LC_ALL, "");
  /* initialize random numbers */
  struct timeval tv;
  gettimeofday (&tv, NULL);
  srand48 (tv.tv_usec + (tv.tv_sec << 16));
  srand (tv.tv_usec + (tv.tv_sec << 16));

  // initialize threading and GLib types
  Rapicorn::ThreadInfo::self().name ("Beast GUI");
  Bse::TaskRegistry::add (Rapicorn::ThreadInfo::self().name(), Rapicorn::ThisThread::process_pid(), Rapicorn::ThisThread::thread_pid());

  /* initialize Birnet/Sfi */
  sfi_init (&argc, argv, "BEAST");
  /* ensure SFI can wake us up */

  // early arg parsing without remote calls
  bst_args_parse_early (&argc, argv);

  // startup BSE, allow remote calls
  Bse::String bseoptions = Bse::string_format ("debug-extensions=%d", bst_debug_extensions);
  Bse::init_async (&argc, argv, "BEAST", Bse::string_split (bseoptions, ":")); // initializes Bse AIDA connection
  // now that the BSE thread runs, drop scheduling priorities if we have any
  setpriority (PRIO_PROCESS, getpid(), 0);
  // hook up Bse aida IDL with main loop
  bst_init_aida_idl();
  // Setup SFI glue context and wakeup
  sfi_glue_context_push (Bse::init_glue_context ("BEAST", bst_main_loop_wakeup));
  source = g_source_simple (GDK_PRIORITY_EVENTS, // G_PRIORITY_HIGH - 100,
			    (GSourcePending) sfi_glue_context_pending,
			    (GSourceDispatch) sfi_glue_context_dispatch,
			    NULL, NULL, NULL);
  g_source_attach (source, NULL);
  g_source_unref (source);

  // arg processing with BSE available, --help, --version
  bst_args_process (&argc, argv);

  // initialize Gtk+ and go into threading mode
  gtk_init (&argc, &argv);
  GDK_THREADS_ENTER ();
  /* initialize Gtk+ Extension Kit */
  gxk_init ();
  /* documentation search paths */
  gxk_text_add_tsm_path (bse_installpath (BSE_INSTALLPATH_DOCDIR).c_str());
  gxk_text_add_tsm_path (bse_installpath (BSE_INSTALLPATH_DATADIR_IMAGES).c_str());
  gxk_text_add_tsm_path (".");
  /* now, we can popup the splash screen */
  beast_splash = bst_splash_new ("BEAST-Splash", BST_SPLASH_WIDTH, BST_SPLASH_HEIGHT, 15);
  bst_splash_set_title (beast_splash, _("BEAST Startup"));
  gtk_object_set_user_data (GTK_OBJECT (beast_splash), NULL);	/* fix for broken user_data in 2.2 */
  bst_splash_set_text (beast_splash,
		       Rapicorn::string_format ("<b><big>BEAST</big></b>\n"
                                                "<b>The BSE Equipped Audio Synthesizer and Tracker</b>\n"
                                                "<b>Version %s (%s)</b>\n",
                                                BST_VERSION, BST_VERSION_HINT));
  bst_splash_update_entity (beast_splash, _("Startup"));
  bst_splash_show_grab (beast_splash);

  main_init_bst_systems();
  main_load_rc_files();
  main_show_splash_image();
  main_init_core_plugins();
  main_init_ladspa();
  main_sleep4gdb();
  main_init_scripts();
  main_init_dialogs();
  BstApp *app = main_open_files (argc - 1, &argv[1]);
  if (!app)
    app = main_open_default_window();
  main_show_release_notes();
  main_splash_down();
  main_run_event_loops();
  main_save_rc_files();
  main_cleanup();

  return 0;
}

static void
main_init_bst_systems()
{
  bst_splash_update_item (beast_splash, _("Initializers"));
  _bst_init_utils ();
  _bst_init_params ();
  _bst_gconfig_init ();
  _bst_skin_config_init ();
  _bst_msg_absorb_config_init ();
}

static void
main_load_rc_files()
{'fraction_non_alphanumeric': 0.11919839679358718, 'fraction_numerical': 0.004168336673346693, 'mean_word_length': 3.8563643441027637, 'pattern_counts': {'":': 4, '<': 35, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6232719', 'n_tokens_mistral': 4473, 'n_tokens_neox': 4321, 'n_words': 968}