int ol_lrclib_init (const char *filename) { ol_log_func (); if (db != NULL) { ol_error ("Lrclib has been initialized."); return 1; } int code = 0; code = sqlite3_open (filename, &db); if (code != SQLITE_OK) { _show_error (); } else { code = sqlite3_exec (db, CREATE_TABLE, NULL, NULL, &errmsg); if (code != SQLITE_OK) { ol_error (errmsg); sqlite3_free (errmsg); } } return db != NULL; }
static int32_t ol_player_xmms2_get_currend_id () { /* ol_log_func (); */ if (!ol_player_xmms2_ensure_connection ()) return 0; int32_t ret = 0; xmmsc_result_t *result = xmmsc_playback_current_id (connection); xmmsc_result_wait (result); xmmsv_t *return_value = xmmsc_result_get_value (result); if (xmmsv_is_error (return_value)) { ol_error ("Error on getting current id"); ret = 0; } else { if (!xmmsv_get_int (return_value, &ret)) { ol_error ("Get id from result failed"); ret = 0; } } xmmsc_result_unref (result); return ret; }
static gboolean ol_player_listen_get_music_info (OlMusicInfo *info) { /* ol_log_func (); */ ol_assert (info != NULL); if (connection == NULL || proxy == NULL) if (!ol_player_listen_init_dbus ()) { ol_debug ("Initialize dbus proxy failed\n"); return FALSE; } gchar *buf; enum OlPlayerStatus status = ol_player_listen_get_status (); ol_debugf (" status: %d\n", (int)status); if (status == OL_PLAYER_PLAYING) { ol_music_info_clear (info); /* gets the title of current music */ if (!ol_dbus_get_string (proxy, get_title, &info->title)) { ol_error (" Get title failed"); } /* gets the artist of current music */ if (!ol_dbus_get_string (proxy, get_artist, &info->artist)) { ol_error (" Get artist failed"); } /* gets the album of current music */ if (!ol_dbus_get_string (proxy, get_album, &info->album)) { ol_error (" Get album failed"); } /* gets the location of the file */ if (!ol_dbus_get_string (proxy, get_uri, &info->uri)) { ol_error (" Get track number failed"); } return TRUE; } else if (status == OL_PLAYER_STOPPED) { return TRUE; } else { ol_errorf (" unknown status\n"); return FALSE; } }
static gboolean ol_player_xmms2_get_music_length (int *len) { /* ol_log_func (); */ ol_assert_ret (len != NULL, FALSE); if (!ol_player_xmms2_ensure_connection ()) return FALSE; *len = 0; int32_t id = ol_player_xmms2_get_currend_id (); if (id > 0) { xmmsc_result_t *result = xmmsc_medialib_get_info (connection, id); xmmsc_result_wait (result); xmmsv_t *return_value = xmmsc_result_get_value (result); if (xmmsv_is_error (return_value)) { ol_error ("Get music info from XMMS2 failed."); } else { xmmsv_t *dict = xmmsv_propdict_to_dict (return_value, NULL); *len = ol_player_xmms2_get_dict_int (return_value, "duration"); xmmsv_unref (dict); } xmmsc_result_unref (result); } return TRUE; }
int ol_is_running () { ol_log_func (); int ret = 1; char *dir = g_strdup_printf ("%s/%s/", g_get_user_config_dir (), PACKAGE_NAME); if (g_mkdir_with_parents (dir, 0755) == -1) { ol_error ("Failed to endure config dir"); } else { char *path = g_strdup_printf ("%s/%s/%s", g_get_user_config_dir (), PACKAGE_NAME, LOCK_FILENAME); int fd = open (path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); if (fd == -1) { ol_error ("Failed to open or create singleton lock"); } else { if (_lockfile (fd) < 0) { if (errno == EACCES || errno == EAGAIN) { close (fd); } ol_infof ("Can not lock file %s: %s\n", path, strerror (errno)); } else { if (ftruncate (fd, 0) != 0) { ol_errorf ("Failed to truncate singleton lock: %s\n", strerror (errno)); } char buf[16]; sprintf (buf, "%ld", (long)getpid ()); if (write (fd, buf, strlen (buf)) != strlen (buf)) ol_errorf ("Failed to write pid in singleton lock\n"); ret = 0; } } g_free (path); } g_free (dir); return ret; }
void ol_player_free (struct OlPlayer *player) { ol_assert (player != NULL); if (player->cmdline != NULL) ol_error ("cmdline is not NULL, this may cause memory leak"); g_free (player); }
void ol_lrclib_unload () { ol_log_func (); if (db == NULL) { ol_error ("Lrclib database is not open"); } else { sqlite3_close (db); db = NULL; } }
static void _initialize (int argc, char **argv) { ol_log_func (); #if ENABLE_NLS /* Set the text message domain. */ bindtextdomain (PACKAGE, LOCALEDIR); bind_textdomain_codeset(PACKAGE, "UTF-8"); /* textdomain (PACKAGE); */ #endif /* Handler for SIGCHLD to wait lrc downloading process */ /* signal (SIGCHLD, child_handler); */ g_thread_init(NULL); gtk_init (&argc, &argv); _parse_cmd_args (&argc, &argv); if (ol_is_running ()) { printf ("%s\n", _("Another OSD Lyrics is running, exit.")); exit (0); } ol_stock_init (); ol_player_init (); /* Initialize display modules */ ol_display_module_init (); OlConfig *config = ol_config_get_instance (); display_mode = ol_config_get_string (config, "General", "display-mode"); module = ol_display_module_new (display_mode); g_signal_connect (config, "changed", G_CALLBACK (_on_config_changed), NULL); ol_trayicon_inital (); ol_notify_init (); ol_keybinding_init (); ol_lrc_fetch_module_init (); char *lrcdb_file = g_strdup_printf ("%s/%s/%s", g_get_user_config_dir (), PACKAGE_NAME, LRCDB_FILENAME); if (!ol_lrclib_init (lrcdb_file)) { ol_error ("Initialize lrclib failed"); } g_free (lrcdb_file); ol_lrc_fetch_add_async_download_callback (_download_callback); refresh_source = g_timeout_add (REFRESH_INTERVAL, _refresh_music_info, NULL); info_timer = g_timeout_add (INFO_INTERVAL, _refresh_player_info, NULL); }
struct OlDisplayModule* ol_display_module_new (const char *name) { ol_assert_ret (name != NULL, NULL); struct OlDisplayModule *module = NULL; struct OlDisplayClass *klass = _get_class (name); if (klass == NULL) { ol_error ("Module %s not exists.\n", name); return NULL; } module = g_new0 (struct OlDisplayModule, 1); module->klass = klass; _set_data (module, klass->init (module)); return module; }
gchar* ol_config_proxy_get_string (OlConfigProxy *config, const gchar *key) { gchar *ret; switch (ol_config_proxy_get (config, "GetString", key, "(s)", &ret)) { case GET_RESULT_OK: return ret; case GET_RESULT_MISSING: return NULL; default: ol_error ("Unknown return value from ol_config_proxy_get"); return NULL; } }
gint ol_config_proxy_get_int (OlConfigProxy *config, const gchar *key) { gint ret; switch (ol_config_proxy_get (config, "GetInt", key, "(i)", &ret)) { case GET_RESULT_OK: return ret; case GET_RESULT_MISSING: case GET_RESULT_FAILED: return 0; default: ol_error ("Unknown return value from ol_config_proxy_get"); return 0; } }
gdouble ol_config_proxy_get_double (OlConfigProxy *config, const gchar *key) { gdouble ret; switch (ol_config_proxy_get (config, "GetDouble", key, "(d)", &ret)) { case GET_RESULT_OK: return ret; case GET_RESULT_MISSING: case GET_RESULT_FAILED: return 0.0; default: ol_error ("Unknown return value from ol_config_proxy_get"); return 0.0; } }
gboolean ol_config_proxy_get_bool (OlConfigProxy *config, const gchar *key) { gboolean ret; switch (ol_config_proxy_get (config, "GetBool", key, "(b)", &ret)) { case GET_RESULT_OK: return ret; case GET_RESULT_MISSING: case GET_RESULT_FAILED: return FALSE; default: ol_error ("Unknown return value from ol_config_proxy_get"); return FALSE; } }
static gboolean ol_player_xmms2_init () { ol_log_func (); ol_debug ("init XMMS2"); if (connection == NULL) { connection = xmmsc_init ("osd-lyrics"); if (!connection) { ol_error ("Initialize XMMS2 connection failed."); return FALSE; } } ol_debug ("Now connect"); return ol_player_xmms2_connect (); }
static gboolean ol_player_xmms2_get_music_info (OlMusicInfo *info) { /* ol_log_func (); */ ol_assert_ret (info != NULL, FALSE); if (!ol_player_xmms2_ensure_connection ()) return FALSE; ol_music_info_clear (info); int32_t id = ol_player_xmms2_get_currend_id (); if (id > 0) { xmmsc_result_t *result = xmmsc_medialib_get_info (connection, id); xmmsc_result_wait (result); xmmsv_t *return_value = xmmsc_result_get_value (result); if (xmmsv_is_error (return_value)) { ol_error ("Get music info from XMMS2 failed."); } else { xmmsv_t *dict = xmmsv_propdict_to_dict (return_value, NULL); info->title = ol_player_xmms2_get_dict_string (dict, "title"); info->artist = ol_player_xmms2_get_dict_string (dict, "artist"); info->album = ol_player_xmms2_get_dict_string (dict, "album"); info->track_number = ol_player_xmms2_get_dict_int (dict, "tracknr"); info->uri = ol_player_xmms2_get_dict_string (dict, "url"); ol_logf (OL_DEBUG, "%s\n" " title:%s\n" " artist:%s\n" " album:%s\n" " uri:%s\n", __FUNCTION__, info->title, info->artist, info->album, info->uri); xmmsv_unref (dict); } xmmsc_result_unref (result); } return TRUE; }
static gboolean ol_player_xmms2_get_played_time (int *played_time) { /* ol_log_func (); */ ol_assert_ret (played_time != NULL, FALSE); if (!ol_player_xmms2_ensure_connection ()) return FALSE; xmmsc_result_t *result = xmmsc_playback_playtime (connection); xmmsc_result_wait (result); xmmsv_t *return_value = xmmsc_result_get_value (result); if (xmmsv_is_error (return_value)) { ol_error ("Get played time from XMMS2 failed"); return FALSE; } int32_t elapsed = 0; xmmsv_get_int (return_value, &elapsed); *played_time = elapsed; xmmsc_result_unref (result); /* ol_debugf ("time: %d\n", *played_time); */ return TRUE; }
gboolean ol_traverse_dir (const char *dirname, gboolean recursive, gboolean (*traverse_func) (const char *path, const char *filename, gpointer userdata), gpointer userdata) { ol_assert_ret (dirname != NULL, FALSE); ol_assert_ret (traverse_func != NULL, FALSE); GError *error = NULL; GDir *dir = g_dir_open (dirname, 0, &error); if (!dir) { ol_error ("Cannot open directory %s: %s\n", dirname, error->message); return FALSE; } const gchar *filename = NULL; while ((filename = g_dir_read_name (dir)) != NULL) { if (!traverse_func (dirname, filename, userdata)) return FALSE; if (recursive) { gchar *filepath = g_build_path (G_DIR_SEPARATOR_S, dirname, filename, NULL); if (g_file_test (filepath, G_FILE_TEST_IS_DIR)) if (!ol_traverse_dir (filepath, recursive, traverse_func, userdata)) { g_free (filepath); return FALSE; } g_free (filepath); } } return TRUE; }
int ol_lrclib_assign_lyric (const OlMusicInfo *info, const char *lrcpath) { ol_log_func (); static char title_value[FIELD_BUFLEN] = ""; static char artist_value[FIELD_BUFLEN] = ""; static char album_value[FIELD_BUFLEN] = ""; static char uri_value[FIELD_BUFLEN] = ""; static char lrcpath_value[FIELD_BUFLEN] = ""; static char where[FIELD_BUFLEN] = ""; static char query[QUERY_BUFLEN] = ""; if (db == NULL) { ol_error ("LrcLib is no initialized."); return 0; } ol_assert_ret (info != NULL, 0); if (ol_music_info_get_title (info) == NULL && ol_music_info_get_uri (info) == NULL) { ol_error ("Require either title or uri be set."); return 0; } _copy_str (lrcpath_value, lrcpath, FIELD_BUFLEN); char *oldpath = NULL; int find_ret = -1; if (info->uri != NULL || (find_ret = ol_lrclib_find (info, &oldpath)) == 0) { _copy_str (title_value, info->title, FIELD_BUFLEN); _copy_str (artist_value, info->artist, FIELD_BUFLEN); _copy_str (album_value, info->album, FIELD_BUFLEN); _copy_str (uri_value, info->uri, FIELD_BUFLEN); snprintf (query, QUERY_BUFLEN, ASSIGN_LYRIC, title_value, artist_value, album_value, info->track_number, uri_value, lrcpath_value); ol_debugf ("query: %s\n", query); int code = sqlite3_exec (db, query, NULL, NULL, &errmsg); if (code != SQLITE_OK) { ol_errorf ("Assign Lyric Failed: %s\n", errmsg); sqlite3_free (errmsg); } return code == SQLITE_OK; } else { if (oldpath != NULL) g_free (oldpath); int retval = _set_where_with_info (info, where, FIELD_BUFLEN); if (retval == -1) return 0; snprintf (query, QUERY_BUFLEN, UPDATE_LYRIC, lrcpath_value, where); ol_debugf ("update query: %s\n", query); int code = sqlite3_exec (db, query, NULL, NULL, &errmsg); if (code != SQLITE_OK) { ol_errorf ("Update Lyric Failed %s\n", errmsg); sqlite3_free (errmsg); } return code == SQLITE_OK; } }
GList * ol_lyric_source_list_sources (OlLyricSource* source) { ol_assert_ret (OL_IS_LYRIC_SOURCE (source), NULL); GList *list = NULL; GVariant *ret; GError *error = NULL; ret = g_dbus_proxy_call_sync (G_DBUS_PROXY (source), "ListSources", NULL, /* parameters */ G_DBUS_CALL_FLAGS_NO_AUTO_START, -1, NULL, &error); if (ret) { GVariantIter *iter; g_variant_get (ret, "(aa{sv})", &iter); GVariantIter *dictiter; while (g_variant_iter_loop (iter, "a{sv}", &dictiter)) { GVariant *value; gchar *key; gchar *name = NULL, *id = NULL; gboolean enabled = FALSE; while (g_variant_iter_loop (dictiter, "{sv}", &key, &value)) { if (strcmp (key, "id") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING)) { id = g_variant_dup_string (value, NULL); } else if (strcmp (key, "name") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING)) { name = g_variant_dup_string (value, NULL); } else if (strcmp (key, "enabled") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_BOOLEAN)) { enabled = g_variant_get_boolean (value); } } if (id == NULL || name == NULL) { ol_error ("Missing id or name in lyric source info"); } else { list = g_list_prepend (list, ol_lyric_source_info_new (id, name, enabled)); } g_free (id); g_free (name); } g_variant_iter_free (iter); list = g_list_reverse (list); g_variant_unref (ret); return list; } else { ol_errorf ("Fail to get lyric source list: %s\n", error->message); g_error_free (error); return NULL; } }