📄 shellthread.cpp
字号:
#include "common.h"#include <pthread.h>#include "havana.h"#include "display.h"static RMcoreList g_playlist;static void ascii_to_uint8 (const RMascii *str, RMuint8 *uint8){ static unsigned int uint; sscanf (str, "%u", &uint); *uint8 = uint;}static void ascii_to_uint16 (const RMascii *str, RMuint16 *uint16){ static unsigned int uint; sscanf (str, "%u", &uint); *uint16 = uint;}static void debug_cmd (void*userData){ RMTdvdControl ctrl_dvd = (RMTdvdControl) userData; RMFDVDDebug (ctrl_dvd, 0, NULL);}static void menuid_cmd (RMuint32 val, void *userData){ RMTdvdControl ctrl_dvd = (RMTdvdControl) userData; RMdvdMenuId menuId = (RMdvdMenuId) val; RMstatus status = RMFDVDMenu_Call (ctrl_dvd, menuId); if (status != RM_OK) { printf ("Menu Call failed\n"); }}static void ttplay_cmd (RMuint32 val, void *userData){ RMTdvdControl ctrl_dvd = (RMTdvdControl) userData; RMuint8 ttn = val & 0xff; RMstatus status = RMFDVDTitle_Play (ctrl_dvd, ttn); if (status != RM_OK) { prepare_start_play (); status = RMFDVDTitle_Play (ctrl_dvd, ttn); if (status != RM_OK) { display_message ("Title Play failed"); } }}static void pttplay_cmd (RMuint32 val1, RMuint32 val2, void *userData){ RMTdvdControl ctrl_dvd = (RMTdvdControl) userData; RMuint8 ttn = val1 & 0xff; RMuint8 pttn = val2 & 0xff; RMstatus status = RMFDVDPTT_Play (ctrl_dvd, ttn, pttn); if (status != RM_OK) { prepare_start_play (); status = RMFDVDPTT_Play (ctrl_dvd, ttn, pttn); if (status != RM_OK) { display_message ("PTT Play failed"); } }}static void pttsearch_cmd (RMuint32 val1, void *userData){ RMTdvdControl ctrl_dvd = (RMTdvdControl) userData; RMuint8 pttn = val1 & 0xff; RMstatus status = RMFDVDPTT_Search (ctrl_dvd, pttn); if (status != RM_OK) { printf ("PTT Search failed\n"); }}static RMstatus string_to_time (const RMascii *str, RMuint32 *const time){ if (strlen (str) != 8) { return RM_ERROR; } RMascii *copy = strdup (str); copy[2] = '\0'; copy[5] = '\0'; RMuint8 hour; ascii_to_uint8 (copy, &hour); RMuint8 minutes; ascii_to_uint8 (copy+3, &minutes); RMuint8 seconds; ascii_to_uint8 (copy+6, &seconds); *time = hour*3600 + minutes * 60 + seconds; *time *= 10; free (copy); return RM_OK;}static void time_search_cmd (const RMascii *str, void *userData){ RMTdvdControl ctrl_dvd = (RMTdvdControl) userData; RMuint32 time; RMstatus status = string_to_time (str, &time); if (status != RM_OK) { return; } status = RMFDVDTime_Search (ctrl_dvd, time); if (status != RM_OK) { printf ("Time Search failed\n"); }}static void repeat_a_cmd (void *userData){ RMTdvdControl ctrl_dvd = (RMTdvdControl) userData; RMstatus status = RMFDVDRepeat_AB (ctrl_dvd); if (status != RM_OK) { printf ("Repeat A failed\n"); }}static void repeat_b_cmd (void *userData){ RMTdvdControl ctrl_dvd = (RMTdvdControl) userData; RMstatus status = RMFDVDRepeat_AB (ctrl_dvd); if (status != RM_OK) { printf ("Repeat B failed\n"); }}static void repeat_ab_off_cmd (void *userData){ RMTdvdControl ctrl_dvd = (RMTdvdControl) userData; RMstatus status = RMFDVDClear_Repeat_AB (ctrl_dvd); if (status != RM_OK) { printf ("Repeat AB off failed\n"); }}static void repeat_chapter_on_cmd (void *userData){ RMTdvdControl ctrl_dvd = (RMTdvdControl) userData; RMstatus status = RMFDVDRepeat_Chapter_On (ctrl_dvd); if (status != RM_OK) { printf ("Repeat Chapter failed\n"); }}static void repeat_chapter_off_cmd (void *userData){ RMTdvdControl ctrl_dvd = (RMTdvdControl) userData; RMstatus status = RMFDVDRepeat_Chapter_Off (ctrl_dvd); if (status != RM_OK) { printf ("Repeat Chapter off failed\n"); }}static void repeat_title_on_cmd (void *userData){ RMTdvdControl ctrl_dvd = (RMTdvdControl) userData; RMstatus status = RMFDVDRepeat_Title_On (ctrl_dvd); if (status != RM_OK) { printf ("Repeat Title failed\n"); }}static void repeat_title_off_cmd (void *userData){ RMTdvdControl ctrl_dvd = (RMTdvdControl) userData; RMstatus status = RMFDVDRepeat_Title_Off (ctrl_dvd); if (status != RM_OK) { printf ("Repeat Title Off failed\n"); }}static void bookmark_save_cmd (const RMascii *str, void *userData){ RMTdvdControl ctrl_dvd = (RMTdvdControl) userData; RMuint8 bookmark[RMFDVDBOOKMARK_SIZE]; RMstatus status = RMFDVDStore_Bookmark (ctrl_dvd, bookmark); if (status != RM_OK) { printf ("Could not save bookmark\n"); return; } FILE *file = fopen (str, "w"); if (file == NULL) { printf ("Could not open file \"%s\" to save the bookmark.\n", str); return; } size_t read = fwrite (bookmark, 1, RMFDVDBOOKMARK_SIZE, file); if (read != RMFDVDBOOKMARK_SIZE) { printf ("Failure saving bookmark to file \"%s\". Wrote only %d bytes.\n", str, read); return; } int close_val = fclose (file); if (close_val != 0) { printf ("Failure closing file \"%s\" for bookmark.\n", str); }}static void bookmark_restore_cmd (const RMascii *str, void *userData){ RMTdvdControl ctrl_dvd = (RMTdvdControl) userData; RMuint8 bookmark[RMFDVDBOOKMARK_SIZE]; FILE *file = fopen (str, "r"); if (file == NULL) { printf ("Could not open file \"%s\" to read the bookmark.\n", str); return; } size_t read = fread (bookmark, 1, RMFDVDBOOKMARK_SIZE, file); if (read != RMFDVDBOOKMARK_SIZE) { printf ("Failure reading bookmark from file \"%s\".\n", str); return; } int close_val = fclose (file); if (close_val != 0) { printf ("Failure closing file \"%s\" for bookmark.\n", str); return; } RMstatus status = RMFDVDResume_Bookmark (ctrl_dvd, bookmark); if (status != RM_OK) { printf ("Failure restoring bookmark\n"); }}static const RMascii *get_next_token (const RMascii *stri, RMascii **next){ RMascii *str = const_cast <RMascii *> (stri); if (str == 0) { return (const RMascii *)NULL; } // get rid of spaces in front. while (*str == ' ') {str++;} if (*str == '\0') { return (const RMascii *)NULL; } // get token. RMascii *tmp = str; while (*str != ' ' && *str != '\0') {str++;} if (str == tmp) { return (const RMascii *)NULL; } *str = '\0'; // get rid of trailing spaces. str++; while (*str == ' ') {str++;} if (*str == '\0') { *next = (RMascii *)NULL; } else { *next = str; } return tmp;}static void get_nextlist_item (RMdvdPlaylistItem *item, const RMascii *str, RMascii **nextStr){ RMascii *tmp; const RMascii *token; token = get_next_token (str, &tmp); if (token == 0) { item->type = RM_DVD_PLAYLIST_END; return; } RMDBGLOG ((ENABLE, "Token: %s\n", token)); if (strncasecmp (token, "ptt", 3) == 0) { // ptt. item->type = RM_DVD_PLAYLIST_PLAY_CHAPTER; token = get_next_token (tmp, &tmp); RMDBGLOG ((ENABLE, "uint Token: %s\n", token)); ascii_to_uint8 (token, &(item->TTN)); token = get_next_token (tmp, &tmp); RMDBGLOG ((ENABLE, "uint Token: %s\n", token)); ascii_to_uint16 (token, &(item->PTTN)); *nextStr = tmp; } else if (strncasecmp (str, "tt", 2) == 0) { // tt. item->type = RM_DVD_PLAYLIST_PLAY_TITLE; token = get_next_token (tmp, &tmp); ascii_to_uint8 (token, &(item->TTN)); RMDBGLOG ((ENABLE, "uint Token: %s\n", token)); *nextStr = tmp; } else { // error. item->type = RM_DVD_PLAYLIST_END; return; }}static void playlist_cmd (const RMascii *str, void *userData){ RMTdvdControl ctrl_dvd = (RMTdvdControl) userData; RMascii *tmp; RMdvdPlaylistItem *item; g_playlist = RMlocalCreateCoreList (); while (1) { item = (RMdvdPlaylistItem *)RMMalloc (sizeof (RMdvdPlaylistItem)); get_nextlist_item (item, str, &tmp); if (item->type == RM_DVD_PLAYLIST_END) { break; } str = tmp; RMlocalInsertLastCoreList (g_playlist, item); } void *firstItem; RMstatus status = RMlocalRemoveFirstCoreList (g_playlist, &firstItem); if (status != RM_OK) { printf ("Could not get first item in playlist.\n"); RMlocalDeleteCoreList (g_playlist); g_playlist = 0; return; } status = RMFDVDPlaylist_On (ctrl_dvd, (RMdvdPlaylistItem *)firstItem); if (status != RM_OK) { prepare_start_play (); status = RMFDVDPlaylist_On (ctrl_dvd, (RMdvdPlaylistItem *)firstItem); if (status != RM_OK) { display_message ("Failure enabling playlist"); } }}static void shell_init (void){ RMShellAddCommandUInt32 ("play-tt", "play-tt [ttn]", "Play Title specified by [ttn].", ttplay_cmd); RMShellAddCommandUInt32UInt32 ("play-ptt", "play-ptt [ttn] [pttn]", "Play Chapter specified by [pttn] in title specified by [ttn].", pttplay_cmd); RMShellAddCommandUInt32 ("search-ptt", "search-ptt [pttn]", "Play Chapter specified by [pttn] in current title.", pttsearch_cmd); RMShellAddCommandUInt32 ("menu-id", "menu-id [menuid]", "Goto menu identified by [menuid]. " "title (2), root (3), audio (5), sp (4), angle (6), ptt (7).", menuid_cmd); RMShellAddCommandString ("time-search", "time-search HH:MM:SS", "Go to time HH:MM:SS in current title.", time_search_cmd); RMShellAddCommandVoid ("repeat-a", "repeat-a", "Set A point for Repeat A/B", repeat_a_cmd); RMShellAddCommandVoid ("repeat-b", "repeat-b", "Set B point for Repeat A/B", repeat_b_cmd); RMShellAddCommandVoid ("repeat-ab-off", "repeat-ab-off", "Disable Repeat A/B", repeat_ab_off_cmd); RMShellAddCommandVoid ("repeat-chapter-on", "repeat-chapter-on", "Repeat Chapter On", repeat_chapter_on_cmd); RMShellAddCommandVoid ("repeat-chapter-off", "repeat-chapter-off", "Repeat Chapter Off", repeat_chapter_off_cmd); RMShellAddCommandVoid ("repeat-title-on", "repeat-title-on", "Repeat Title On", repeat_title_on_cmd); RMShellAddCommandVoid ("repeat-title-off", "repeat-title-off", "Repeat Title Off", repeat_title_off_cmd); RMShellAddCommandString ("bookmark-save", "bookmark-save file", "Save current position in file.bookmark.", bookmark_save_cmd); RMShellAddCommandString ("bookmark-restore", "bookmark-restore file", "Restore current position in file.bookmark.", bookmark_restore_cmd); RMShellAddCommandString ("playlist", "playlist list", "playback of playlist specified by list. list is a list of \"[tt|ptt] ttn pttn\" arguments.", playlist_cmd); RMShellAddCommandVoid ("debug", "debug", "debug", debug_cmd);}static pthread_t g_thread;static pthread_mutex_t g_mutex;static int g_must_die;static void *my_local_thread (void *p){ RMTdvdControl *ctrl_dvd = (RMTdvdControl *)p; RMShellInit ("Havana DVD Test Application", ctrl_dvd); shell_init (); while (1) { usleep (100000); // sleep some time. shell_thread_lock (); RMShellRun (); shell_thread_unlock (); if (g_must_die) { RMShellDeInit (); break; } } return NULL;}void shell_thread_create (RMTdvdControl ctrl_dvd){ g_must_die = FALSE; pthread_mutex_init (&g_mutex, NULL); pthread_create (&g_thread, NULL, my_local_thread, (void *)ctrl_dvd);}void shell_thread_lock (void){ pthread_mutex_lock (&g_mutex);}const char *shell_thread_get_user_input (void){ static char str[20]; scanf ("%s\n", str); return str;}void shell_thread_unlock (void){ pthread_mutex_unlock (&g_mutex);}void shell_thread_die (void){ g_must_die = TRUE; pthread_join (g_thread, NULL); pthread_mutex_destroy (&g_mutex);}RMstatus shell_thread_get_next_playlist (RMdvdPlaylistItem **item){ RMstatus status = RMlocalRemoveFirstCoreList (g_playlist, ((void **)item)); return status;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -