⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 showlyricapp.c

📁 ShowLyric是一款用于Audacious的歌词显示插件
💻 C
字号:
#include "../include/ShowLyric.h"#include "../include/ShowLyricApp.h"#include <sys/stat.h>#define GLADE_FILE "/usr/share/ShowLyric/ShowLyricUI.glade"#define SEL_LYRIC_GLADE_FILE "/usr/share/ShowLyric/SelLyricDlg.glade"#include "../include/ConfigDlg.h"#include "../include/ShowLyricDlg.h"#include "../include/SelLyricDlg.h"struct _ShowLyricApp theApp ={ 	.Init = Init,	.Clean = Clean,	.SearchLyric = SearchLyric,	.ShowConfigDlg = ShowConfigDlg,	.UpdataSetting = UpdataSetting,	.PreChangeSong = PreChangeSong,	.ChangeSong = ChangeSong,	.SetAppState = SetAppState,	.About = About,	.GetDefautConfigs = GetDefautConfigs,	.DownLoadLyric = DownLoadLyric, };gchar* GetDefaultLyricPath(OUT gchar * lpszPath){	if (!lpszPath)		return NULL;	sprintf(lpszPath, "%s/.lyrics", theApp.m_szCurrUsrPath);	if (access(lpszPath, F_OK) == -1)	{		mkdir(lpszPath, 0755);	}	return lpszPath;}// 获取默认设置void GetDefautConfigs(Configs* pConfig){	pConfig->bSmartDownLoad = TRUE;	pConfig->bSmartShowWin = TRUE;	pConfig->bChangeLyric = TRUE;	pConfig->bCanDrag = TRUE;	pConfig->bAutoSaveLyric = FALSE;	pConfig->bShowBroad = FALSE;	pConfig->bSaveLyricToSongFolder = FALSE;	strcpy(pConfig->szLyricFontName, "Sans 10");	GetDefaultLyricPath(pConfig->szLyricPath);	gdk_color_parse("black", &pConfig->colors.background);	gdk_color_parse("darkblue", &pConfig->colors.normal);	gdk_color_parse("green", &pConfig->colors.current);	gdk_color_parse("yellow", &pConfig->colors.drag);	gdk_color_parse("green", &pConfig->colors.msg);	gdk_color_parse("red", &pConfig->colors.error);}void Init(){	LyricDebug("theApp:Init");	theApp.m_state = AS_Waiting;	struct passwd *my_info = getpwuid(getuid());	strcpy(theApp.m_szCurrUsrPath, my_info->pw_dir);	sprintf(theApp.m_szConfigPath, "%s/.%s", theApp.m_szCurrUsrPath, 	PLUGIN_NAME);	if (access(theApp.m_szConfigPath, F_OK) == -1)	{		mkdir(theApp.m_szConfigPath, 0755);	}	// 写入默认配置	GetDefautConfigs(&theApp.m_configs);	// 加载配置信息	theApp.m_player.LoadConfigs(&theApp.m_configs);	// 初始化界面	theApp.m_xml = glade_xml_new(GLADE_FILE, NULL, NULL);	//glade_xml_signal_autoconnect (theApp.m_xml);	 if (!g_thread_supported()) 	      g_thread_init(NULL); 	gdk_threads_init();	LyricWndInit();	ConfigDlgInit();	UpdataSetting();}// 释放资源void Clean(){	// 释放资源	_FreeList(theApp._m_ListLyrics);}// 显示配置窗口void ShowConfigDlg(){	gtk_widget_show(glade_xml_get_widget(theApp.m_xml, "ConfigDlg"));}// 更新配置信息void UpdataSetting(){	theApp.m_player.SaveConfigs(&theApp.m_configs);	theApp.m_LyricWnd.RefreshLyricSetting();}// 设置程序的状态void SetAppState(enum AppState state){	theApp.m_state = state;	// TODO:在歌词窗口显示当前状态}// 释放链表占用的资源void _FreeList(GList* lpList){	GList* list = g_list_first(lpList);	while (list)	{		g_free(list->data);		list = g_list_next(list);	}	g_list_free(lpList);}// 搜索歌词gboolean SearchLyric(SongInfo info, gboolean bSmartDownLoad){	LyricDebug("SearchLyric......\n");	SetAppState(AS_SearchingLyric);	gchar szTmpFileName[MAX_PATH] =	{ 0 };	sprintf(szTmpFileName, "%s/tmp.txt", theApp.m_szConfigPath);	pid_t child = fork();	if (child == -1)	{		LyricLog("Fork Error!\n");		return FALSE;	}	else if (child == 0)	{		char szArgv[3][512] =		{		{ 0 } };		sprintf(szArgv[0], "-a:\"%s\"", info.szArtist);		sprintf(szArgv[1], "-t:\"%s\"", info.szTitle);		sprintf(szArgv[2], "-o:\"%s\"", szTmpFileName);		execl("/usr/share/ShowLyric/ttplayer", "ttplayer", szArgv[0], szArgv[1], szArgv[2],				(char * )0);		exit(0);	}	LyricDebug("正在搜索歌词......\n");	int iState;	int iCount = 20;	while (iCount--)	{		LyricDebug("(%d)正在搜索歌词(%d)", iCount, child);		pid_t pr = waitpid(child, &iState, WNOHANG);		if (pr > 0)			break;		sleep(1);	}	if (iCount < 0)	{		LyricLog("搜索超时!");		return FALSE;	}	FILE * pFile= NULL;	iCount = 20;	while (iCount--)	{		sleep(1);		pFile = fopen(szTmpFileName, "r");		if (pFile)			break;		LyricLog("(%d)打开文件%s失败!", iCount, szTmpFileName);	}	if (!pFile)	{		LyricLog("打开文件%s失败!", szTmpFileName);		return FALSE;	}	gchar szBuf[1025] =	{ 0 };	fseek(pFile, 0, SEEK_SET);	fread(szBuf, 1, 1024, pFile);	LyricDebug("%s\n------\n", szBuf);	// 解析列表并显示选择窗口	char *pszID= NULL, *pszArtist= NULL, *pszTitle= NULL;	xmlDocPtr xmldoc = xmlParseMemory(szBuf, strlen(szBuf));	if (!xmldoc)	{		LyricLog("Error while searching.\n");		return FALSE;	}	xmlNodePtr lyrics = xmldoc->children;	iCount = 0;	_FreeList(theApp._m_ListLyrics);	theApp._m_ListLyrics = NULL;	GladeXML* xml = glade_xml_new(SEL_LYRIC_GLADE_FILE, NULL, NULL);	SelLyricDlgInit(xml);	GtkWidget * pSelLyricDlg = glade_xml_get_widget(xml, "SelLyricDlg");	GtkWidget * pSelLyricComboxLyrics = glade_xml_get_widget(xml, "SelLyricComboxLyrics");	while (lyrics)	{		xmlNodePtr lyric = lyrics->children;		while (lyric)		{			if (xmlStrcmp(lyric->name, (xmlChar *)"lrc") == 0)			{				pszID = (char *)xmlGetProp(lyric, (xmlChar *)"id");				pszArtist = (char *)xmlGetProp(lyric, (xmlChar *)"artist");				pszTitle = (char *)xmlGetProp(lyric, (xmlChar *)"title");				iCount++;				LyricsListItem *pNewItem = (LyricsListItem *)malloc(sizeof(LyricsListItem));				if (!pNewItem)					break;				strcpy(pNewItem->szID, pszID);				strcpy(pNewItem->szArtist, pszArtist);				strcpy(pNewItem->szTitle, pszTitle);				theApp._m_ListLyrics = g_list_append(theApp._m_ListLyrics, pNewItem);				gtk_combo_box_append_text(GTK_COMBO_BOX(pSelLyricComboxLyrics), pszID);			}			lyric = lyric->next;		}		lyrics = lyrics->next;	}	gboolean bRet = FALSE;	if (g_list_length(theApp._m_ListLyrics) > 0)	{		bRet = TRUE;		if (bSmartDownLoad)		{			GList* first = g_list_first(theApp._m_ListLyrics);			LyricsListItem *pItem = first->data;			DownLoadLyric(pItem->szID, pItem->szTitle, pItem->szArtist);		}		else		{			gtk_combo_box_set_active(GTK_COMBO_BOX(pSelLyricComboxLyrics), 0);			gtk_widget_show(pSelLyricDlg);		}	}	fclose(pFile);	pFile = NULL;	return bRet;}// 加载歌词gboolean _LoadLyric(SongInfo info){	SetAppState(AS_LoadingLyric);	const gchar * pszTitle = info.szTitle;	const gchar * pszArtist = info.szArtist;	LyricDebug("LoadLyric(%s, %s)", pszTitle, pszArtist);#define LRC_PATH_NUM	6	gchar szLrcFile[LRC_PATH_NUM][MAX_PATH];	int i = 0, iFileIndex = -1;	if (theApp.m_configs.bSaveLyricToSongFolder)	{		strcpy(theApp.m_configs.szLyricPath, info.szSongFolder);		if (!theApp.m_configs.szLyricPath || strlen(theApp.m_configs.szLyricPath) <= 0)		{			GetDefaultLyricPath(theApp.m_configs.szLyricPath);		}	}	if (strlen((char *) pszTitle) <= 0)		return FALSE;	sprintf(szLrcFile[0], "%s/%s/%s.lrc", theApp.m_configs.szLyricPath, pszArtist, pszTitle);	sprintf(szLrcFile[1], "%s/%s - %s.lrc", theApp.m_configs.szLyricPath, pszArtist, pszTitle);	sprintf(szLrcFile[2], "%s/%s.lrc", theApp.m_configs.szLyricPath, pszTitle);	szLrcFile[LRC_PATH_NUM - 1][0] = 0;	// 检查该歌曲的歌词是否存在	for (i = 0; i < LRC_PATH_NUM; i++)	{		if (strlen((char *) szLrcFile) == 0)			break;		if (g_file_test(szLrcFile[i], G_FILE_TEST_IS_REGULAR))		{			iFileIndex = i;			break;		}	}	theApp.m_CurrSongInfo = info;	if (iFileIndex >= 0)	{		strcpy(theApp.m_CurrSongInfo.szLyricFileName, szLrcFile[iFileIndex]);		LyricDebug("本地歌词:%s", theApp.m_CurrSongInfo.szLyricFileName);		return theApp.m_LyricWnd.ParseLyric(theApp.m_CurrSongInfo.szLyricFileName);	}	else	{		if (theApp.m_configs.bSaveLyricToSongFolder)		{			strcpy(info.szLyricFileName, szLrcFile[1]);		}		else		{			strcpy(info.szLyricFileName, szLrcFile[0]);		}		theApp.m_CurrSongInfo = info;		return SearchLyric(info, theApp.m_configs.bSmartDownLoad);	}}void DownLoadLyric(const gchar* lpszId, const gchar* lpszTitle, const gchar* lpszArtist){	pid_t child = fork();	if (child == -1)	{		LyricLog("Fork Error!%s:%d\n",__FILE__ , __LINE__);		return;	}	else if (child == 0)	{		char szArgv[4][512];		sprintf(szArgv[0], "-i:\"%s\"", lpszId);		sprintf(szArgv[1], "-t:\"%s\"", lpszTitle);		sprintf(szArgv[2], "-a:\"%s\"", lpszArtist);		sprintf(szArgv[3], "-o:\"%s\"", theApp.m_CurrSongInfo.szLyricFileName);		execl("/usr/share/ShowLyric/ttplayer", "ttplayer", szArgv[0],		szArgv[1], szArgv[2], szArgv[3], (char * )0);		exit(0);	}	LyricDebug("正在下载歌词......\n");	int iState;	int iCount = 30;	while (iCount--)	{		LyricDebug("正在下载歌词(%d)", iCount);		pid_t pr = waitpid(child, &iState, WNOHANG);		if (pr> 0)		break;		sleep(1);	}	if (iCount < 0)	{		LyricLog("下载歌词超时!");		return;	}	LyricDebug("正在下载完毕......\n");	theApp.m_LyricWnd.ParseLyric(theApp.m_CurrSongInfo.szLyricFileName);}gboolean PreChangeSong(SongInfo* pInfo){	/*	 if (theApp.m_state != AS_ShowingLyric && theApp.m_state != AS_Waiting) {	 LyricLog("PreChangeSong:the state is error!\n");	 return FALSE;	 }	 */	theApp.m_LyricWnd.ClearLyric();	GtkWidget *pLyricWnd = glade_xml_get_widget(theApp.m_xml, "ShowLyric");	gtk_window_set_title(GTK_WINDOW(pLyricWnd), PLUGIN_NAME);	gboolean bRet = _LoadLyric(*pInfo);	theApp.m_LyricWnd.SmartShowWnd(bRet);	return bRet;}void ChangeSong(SongInfo info){	GtkWidget *pLyricWnd = glade_xml_get_widget(theApp.m_xml, "ShowLyric");	gtk_window_set_title(GTK_WINDOW(pLyricWnd), info.szTitle);}void About(GtkWindow* pParent){	const char *authors[] =	{ "Allan <qimingos_lsk@163.com>", NULL };	const gchar *license[] =	{ "This is free software; you can redistribute it and/or modify "		"it under the terms of the GNU General Public License as published by "		"the Free Software Foundation; either version 2 of the License, or "		"(at your option) any later version.",			"It is distributed in the hope that it will be useful, "				"but WITHOUT ANY WARRANTY; without even the implied warranty of "				"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the "				"GNU General Public License for more details.",			"You should have received a copy of the GNU General Public License "				"along with this; if not, write to the Free Software Foundation, Inc., "				"51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA" };	gchar *license_text = g_strjoin("\n\n", 	_(license[0]), _(license[1]), _(license[2]), NULL);	gtk_show_about_dialog(pParent, "name", PLUGIN_NAME, "comments", 	PLUGIN_NAME, "program-name", PLUGIN_NAME, "version", VERSION, "authors", authors, "license",			license_text, "wrap-license", TRUE, "translator-credits", _("translator-credits"), "logo-icon-name", PLUGIN_NAME, 			NULL);	g_free(license_text);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -