int main(int argc, char* argv[]) { gint error = 0; options_init(argc - 1, argv + 1); INFO("Starting up."); descriptor_init(); DEBUG("Disabling SIGPIPE."); signal(SIGPIPE, SIG_IGN); DEBUG("Creating ZeroMQ context."); gpointer zmq_context = zmq_init(options_zmq_io_threads()); if (zmq_context == NULL) { PERROR("main(zmq_init)"); error = 1; goto err0; } DEBUG("Creating server on port %s.", options_port()); gint socket = socket_server(options_port()); if (socket == -1) { error = 1; goto err1; } INFO("Initialising Lua API."); lua_api_init(zmq_context, argc - 1, argv + 1); lua_State* lua = lua_api_get(); DEBUG("Running " LUA_START_FILE); if (luaL_dofile(lua, LUA_START_FILE) == 1) { ERROR("%s", lua_tostring(lua, -1)); error = 1; goto err2; } io_mainloop(socket); err2: DEBUG("Closing server socket."); socket_close(socket); err1: DEBUG("Terminating ZeroMQ context."); /* This is separate from lua_api_deinit() to prevent zmq_term() from blocking forever. */ lua_zmq_deinit(); zmq_term(zmq_context); err0: DEBUG("Enabling SIGPIPE."); signal(SIGPIPE, SIG_DFL); options_deinit(); descriptor_deinit(); if (lua_api_get() != NULL) { DEBUG("Closing Lua state."); lua_api_deinit(); } return error; }
static void deinit(void) { // others... sb_deinit(); ti_deinit(); ts_deinit(); tl_deinit(); comm_deinit(); options_deinit(); }
int main(int argc, const char *argv[]) { #ifndef WIN32 struct sigaction act; #endif #ifdef ENABLE_LOCALE #ifndef ENABLE_NLS G_GNUC_UNUSED #endif const char *charset = NULL; #endif GIOChannel *keyboard_channel; #ifdef ENABLE_LIRC int lirc_socket; GIOChannel *lirc_channel = NULL; #endif GIOChannel *sigwinch_channel = NULL; #ifdef ENABLE_LOCALE /* time and date formatting */ setlocale(LC_TIME,""); /* care about sorting order etc */ setlocale(LC_COLLATE,""); /* charset */ setlocale(LC_CTYPE,""); /* initialize charset conversions */ charset = charset_init(); /* initialize i18n support */ #endif #ifdef ENABLE_NLS setlocale(LC_MESSAGES, ""); bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR); #ifdef ENABLE_LOCALE bind_textdomain_codeset(GETTEXT_PACKAGE, charset); #endif textdomain(GETTEXT_PACKAGE); #endif /* initialize options */ options_init(); /* parse command line options - 1 pass get configuration files */ options_parse(argc, argv); #ifndef NCMPC_MINI /* read configuration */ read_configuration(); /* check key bindings */ check_key_bindings(NULL, NULL, 0); #endif /* parse command line options - 2 pass */ options_parse(argc, argv); #ifndef WIN32 /* setup signal behavior - SIGINT */ sigemptyset(&act.sa_mask); act.sa_flags = 0; act.sa_handler = catch_sigint; if (sigaction(SIGINT, &act, NULL) < 0) { perror("signal"); exit(EXIT_FAILURE); } /* setup signal behavior - SIGTERM */ act.sa_handler = catch_sigint; if (sigaction(SIGTERM, &act, NULL) < 0) { perror("sigaction()"); exit(EXIT_FAILURE); } /* setup signal behavior - SIGCONT */ act.sa_handler = catch_sigcont; if (sigaction(SIGCONT, &act, NULL) < 0) { perror("sigaction(SIGCONT)"); exit(EXIT_FAILURE); } /* setup signal behaviour - SIGHUP*/ act.sa_handler = catch_sigint; if (sigaction(SIGHUP, &act, NULL) < 0) { perror("sigaction(SIGHUP)"); exit(EXIT_FAILURE); } /* setup SIGWINCH */ act.sa_flags = SA_RESTART; act.sa_handler = catch_sigwinch; if (sigaction(SIGWINCH, &act, NULL) < 0) { perror("sigaction(SIGWINCH)"); exit(EXIT_FAILURE); } /* ignore SIGPIPE */ act.sa_handler = SIG_IGN; if (sigaction(SIGPIPE, &act, NULL) < 0) { perror("sigaction(SIGPIPE)"); exit(EXIT_FAILURE); } #endif ncu_init(); #ifdef ENABLE_LYRICS_SCREEN lyrics_init(); #endif /* create mpdclient instance */ mpd = mpdclient_new(); /* initialize curses */ screen_init(mpd); /* the main loop */ main_loop = g_main_loop_new(NULL, FALSE); /* watch out for keyboard input */ keyboard_channel = g_io_channel_unix_new(STDIN_FILENO); g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL); #ifdef ENABLE_LIRC /* watch out for lirc input */ lirc_socket = ncmpc_lirc_open(); if (lirc_socket >= 0) { lirc_channel = g_io_channel_unix_new(lirc_socket); g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL); } #endif #ifndef WIN32 if (!pipe(sigwinch_pipes) && !fcntl(sigwinch_pipes[1], F_SETFL, O_NONBLOCK)) { sigwinch_channel = g_io_channel_unix_new(sigwinch_pipes[0]); g_io_add_watch(sigwinch_channel, G_IO_IN, sigwinch_event, NULL); } else { perror("sigwinch pipe creation failed"); exit(EXIT_FAILURE); } #endif /* attempt to connect */ reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL); auto_update_timer(); #ifndef NCMPC_MINI check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL); #endif screen_paint(mpd); g_main_loop_run(main_loop); /* cleanup */ cancel_seek_timer(); disable_update_timer(); if (reconnect_source_id != 0) g_source_remove(reconnect_source_id); #ifndef NCMPC_MINI if (check_key_bindings_source_id != 0) g_source_remove(check_key_bindings_source_id); #endif g_main_loop_unref(main_loop); g_io_channel_unref(keyboard_channel); g_io_channel_unref(sigwinch_channel); close(sigwinch_pipes[0]); close(sigwinch_pipes[1]); #ifdef ENABLE_LIRC if (lirc_socket >= 0) g_io_channel_unref(lirc_channel); ncmpc_lirc_close(); #endif exit_and_cleanup(); #ifdef ENABLE_LYRICS_SCREEN lyrics_deinit(); #endif ncu_deinit(); options_deinit(); return 0; }