2021-02-09 17:37:26 +02:00
/* main.c
*
* Copyright 2021 Joshua Strobl
*
* Licensed under the Apache License , Version 2.0 ( the " License " ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an " AS IS " BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
*/
2021-05-27 12:56:36 +03:00
# include <glib.h>
2021-03-23 19:50:09 +02:00
# include <gstreamer-1.0/gst/gst.h>
2021-06-22 16:48:13 +03:00
# include <magic.h>
# include <taglib/tag_c.h>
2021-05-27 12:56:36 +03:00
# include "config/config.h"
2021-03-16 11:04:04 +02:00
# include "db/cartographer.h"
2021-02-27 17:26:24 +02:00
# include "db/db.h"
2021-06-22 16:48:13 +03:00
# include "db/loaders.h"
2021-07-08 18:37:52 +03:00
# include "indexer/track-helpers.h"
2021-06-22 16:48:13 +03:00
# include "playback/engine.h"
2021-04-06 15:35:01 +03:00
# include "playback/media-keys.h"
2021-04-06 10:41:15 +03:00
# include "playback/mimes.h"
# include "playback/mpris.h"
2021-05-27 12:56:36 +03:00
# include "paths.h"
2021-02-09 17:37:26 +02:00
2021-05-27 12:56:36 +03:00
# include "config/config.h"
# include "koto-paths.h"
2021-02-09 17:37:26 +02:00
# include "koto-window.h"
2021-05-27 12:56:36 +03:00
extern KotoConfig * config ;
2021-04-06 10:41:15 +03:00
extern guint mpris_bus_id ;
2021-05-11 20:05:04 +03:00
extern GDBusNodeInfo * introspection_data ;
2021-04-06 10:41:15 +03:00
2021-06-22 16:48:13 +03:00
extern KotoPlaybackEngine * playback_engine ;
2021-05-11 20:05:04 +03:00
extern KotoCartographer * koto_maps ;
extern sqlite3 * koto_db ;
2021-02-27 17:26:24 +02:00
2021-05-11 20:05:04 +03:00
extern GHashTable * supported_mimes_hash ;
extern GList * supported_mimes ;
2021-04-06 10:41:15 +03:00
2021-05-27 12:56:36 +03:00
extern gchar * koto_path_to_conf ;
extern gchar * koto_rev_dns ;
2021-07-06 13:06:20 +03:00
extern gboolean created_new_db ;
2021-06-22 16:48:13 +03:00
GVolumeMonitor * volume_monitor = NULL ;
2021-05-11 20:05:04 +03:00
GtkApplication * app = NULL ;
GtkWindow * main_window ;
2021-06-22 16:48:13 +03:00
magic_t magic_cookie ;
2021-04-06 10:41:15 +03:00
2021-05-11 20:05:04 +03:00
static void on_activate ( GtkApplication * app ) {
g_assert ( GTK_IS_APPLICATION ( app ) ) ;
2021-02-09 17:37:26 +02:00
2021-05-11 20:05:04 +03:00
main_window = gtk_application_get_active_window ( app ) ;
2021-04-06 10:41:15 +03:00
if ( main_window = = NULL ) {
2021-05-11 20:05:04 +03:00
main_window = g_object_new ( KOTO_TYPE_WINDOW , " application " , app , " default-width " , 1200 , " default-height " , 675 , NULL ) ;
2021-04-06 15:35:01 +03:00
setup_mpris_interfaces ( ) ; // Set up our MPRIS interfaces
setup_mediakeys_interface ( ) ; // Set up our media key support
2021-07-06 13:06:20 +03:00
if ( ! created_new_db ) {
read_from_db ( ) ; // Read the database, allowing us to propagate the UI with various data such as artists and playlist navigation elements
}
2021-02-09 17:37:26 +02:00
}
2021-04-06 10:41:15 +03:00
gtk_window_present ( main_window ) ;
2021-02-09 17:37:26 +02:00
}
2021-05-11 20:05:04 +03:00
static void on_shutdown ( GtkApplication * app ) {
2021-02-27 17:26:24 +02:00
( void ) app ;
2021-05-27 12:56:36 +03:00
koto_config_save ( config ) ; // Save our config
2021-02-27 17:26:24 +02:00
close_db ( ) ; // Close the database
2021-04-06 10:41:15 +03:00
g_bus_unown_name ( mpris_bus_id ) ;
g_dbus_node_info_unref ( introspection_data ) ;
2021-02-27 17:26:24 +02:00
}
2021-05-11 20:05:04 +03:00
int main (
int argc ,
char * argv [ ]
) {
2021-02-09 17:37:26 +02:00
int ret ;
2021-02-25 18:15:36 +02:00
gtk_init ( ) ;
2021-03-23 19:50:09 +02:00
gst_init ( & argc , & argv ) ;
2021-07-08 18:37:52 +03:00
koto_track_helpers_init ( ) ; // Init our track helpers (primarily our genre replacement hashtable)
2021-05-27 12:56:36 +03:00
koto_paths_setup ( ) ; // Set up our required paths
2021-04-06 10:41:15 +03:00
supported_mimes_hash = g_hash_table_new ( g_str_hash , g_str_equal ) ;
supported_mimes = NULL ; // Ensure our mimes GList is initialized
koto_playback_engine_get_supported_mimetypes ( supported_mimes ) ;
2021-03-16 11:04:04 +02:00
koto_maps = koto_cartographer_new ( ) ; // Create our new cartographer and their collection of maps
2021-05-27 12:56:36 +03:00
2021-06-22 16:48:13 +03:00
volume_monitor = g_volume_monitor_get ( ) ; // Get a VolumeMonitor
2021-05-27 12:56:36 +03:00
config = koto_config_new ( ) ; // Set our config
koto_config_load ( config , koto_path_to_conf ) ;
2021-06-22 16:48:13 +03:00
playback_engine = koto_playback_engine_new ( ) ; // Initialize the engine now that the config is available, since it listens on various config signals
taglib_id3v2_set_default_text_encoding ( TagLib_ID3v2_UTF8 ) ; // Ensure our id3v2 text encoding is UTF-8
magic_cookie = magic_open ( MAGIC_MIME ) ;
if ( magic_cookie = = NULL ) { // Failed to open
g_critical ( " Failed to allocate a cookie pointer from libmagic. " ) ;
}
if ( magic_load ( magic_cookie , NULL ) ! = 0 ) { // Failed to load data
magic_close ( magic_cookie ) ;
g_critical ( " Failed to load the system magic database. " ) ;
}
2021-02-27 17:26:24 +02:00
open_db ( ) ; // Open our database
2021-06-22 16:48:13 +03:00
g_thread_new ( " indexing-any-necessary-libs " , ( void * ) koto_config_load_libs , config ) ; // Load our libraries, now that our database is set up. Note that read_from_db is called in koto-window.c
2021-04-06 10:41:15 +03:00
2021-05-27 12:56:36 +03:00
app = gtk_application_new ( koto_rev_dns , G_APPLICATION_FLAGS_NONE ) ;
2021-05-11 20:05:04 +03:00
g_signal_connect ( app , " activate " , G_CALLBACK ( on_activate ) , NULL ) ;
2021-02-27 17:26:24 +02:00
g_signal_connect ( app , " shutdown " , G_CALLBACK ( on_shutdown ) , NULL ) ;
2021-05-11 20:05:04 +03:00
ret = g_application_run ( G_APPLICATION ( app ) , argc , argv ) ;
2021-02-09 17:37:26 +02:00
return ret ;
}