// For licensing and usage information, read docs/release/winui_license.txt //**************************************************************************** /*************************************************************************** treeview.cpp TreeView support routines - MSH 11/19/1998 ***************************************************************************/ // standard windows headers #include #include // standard C headers #include #include // MAME/MAMEUI headers #include "emu.h" #include "mui_util.h" #include "winui.h" #include "treeview.h" #include "resource.h" #include "mui_opts.h" #include "emu_opts.h" #include "dialogs.h" #include "winutf8.h" #include "screen.h" #include "drivenum.h" #include "corestr.h" /*************************************************************************** public structures ***************************************************************************/ #define ICON_MAX (sizeof(treeIconNames) / sizeof(treeIconNames[0])) /* Name used for user-defined custom icons */ /* external *.ico file to look for. */ typedef struct { int nResourceID; LPCSTR lpName; } TREEICON; static TREEICON treeIconNames[] = { { IDI_FP_OPEN, "fp-open" }, { IDI_FP_DEF, "fp-closed" }, { IDI_FP_ALL, "fp-all" }, { IDI_FP_ARCADE, "fp-arcade" }, { IDI_FP_AVAIL, "fp-avail" }, { IDI_FP_BIOS, "fp-bios" }, { IDI_FP_CLONES, "fp-clone" }, { IDI_FP_CPU, "fp-cpu" }, { IDI_FP_CUSTOM, "custom" }, { IDI_FP_DUMP, "fp-dump" }, { IDI_FP_FPS, "fp-fps" }, { IDI_FP_HARDDISK, "fp-hard" }, { IDI_FP_HORI, "fp-hori" }, { IDI_FP_IMP, "fp-imp" }, { IDI_FP_LIGHTGUN, "fp-lgun" }, { IDI_FP_MANU, "fp-manu" }, { IDI_FP_MECH, "fp-mech" }, { IDI_FP_MODIFIED, "fp-modi" }, { IDI_FP_MONITOR, "fp-monit" }, { IDI_FP_MOUSE, "fp-mouse" }, { IDI_FP_NONMECH, "fp-nmech" }, { IDI_FP_NW, "fp-nw" }, { IDI_FP_PARENTS, "fp-parent" }, { IDI_FP_RASTER, "fp-raster" }, { IDI_FP_RESOL, "fp-resol" }, { IDI_FP_SAMPLES, "fp-sample" }, { IDI_FP_SAVESTATE, "fp-savest" }, { IDI_FP_SOUND, "fp-sound" }, { IDI_FP_SOURCE, "fp-source" }, { IDI_FP_STEREO, "fp-stereo" }, { IDI_FP_TRACKBALL, "fp-track" }, { IDI_FP_UNAVAIL, "fp-unav" }, { IDI_FP_VECTOR, "fp-vector" }, { IDI_FP_VERT, "fp-vert" }, { IDI_FP_W, "fp-w" }, { IDI_FP_YEAR, "fp-year" }, { IDI_FC_BIOS, "fc-bios" }, { IDI_FC_CHIP, "fc-chip" }, { IDI_FC_CPU, "fc-cpu" }, { IDI_FC_MANU, "fc-manu" }, { IDI_FC_MONITOR, "fc-monit" }, { IDI_FC_SOUND, "fc-sound" }, { IDI_FC_SOURCE, "fc-source" }, { IDI_FC_YEAR, "fc-year" }, }; /*************************************************************************** private variables ***************************************************************************/ /* this has an entry for every folder eventually in the UI, including subfolders */ static TREEFOLDER **m_treeFolders = 0; static UINT m_numFolders = 0; /* Number of folder in the folder array */ static UINT m_next_folder_id = MAX_FOLDERS; static UINT m_folderArrayLength = 0; /* Size of the folder array */ static LPTREEFOLDER m_lpCurrentFolder = 0; /* Currently selected folder */ static UINT m_nCurrentFolder = 0; /* Current folder ID */ static WNDPROC m_lpTreeWndProc = 0; /* for subclassing the TreeView */ static HIMAGELIST m_hTreeSmall = 0; /* TreeView Image list of icons */ const UINT m_folderBytes = sizeof(EXFOLDERDATA); /* this only has an entry for each TOP LEVEL extra folder + SubFolders*/ LPEXFOLDERDATA m_ExtraFolderData[MAX_EXTRA_FOLDERS * MAX_EXTRA_SUBFOLDERS]; static int m_numExtraFolders = 0; static int m_numExtraIcons = 0; static int m_numInbuiltFolders = 0; static char *m_ExtraFolderIcons[MAX_EXTRA_FOLDERS]; // built in folders and filters static LPCFOLDERDATA m_lpFolderData; static LPCFILTER_ITEM m_lpFilterList; /*************************************************************************** private function prototypes ***************************************************************************/ extern BOOL InitFolders(); static BOOL CreateTreeIcons(); static void TreeCtrlOnPaint(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); static const char *ParseManufacturer(const char *s, int *pParsedChars ); static BOOL AddFolder(LPTREEFOLDER lpFolder); static LPTREEFOLDER NewFolder(const char *lpTitle, UINT nFolderId, int nParent, UINT nIconId, DWORD dwFlags); static void DeleteFolder(LPTREEFOLDER lpFolder); static LRESULT CALLBACK TreeWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); static int InitExtraFolders(); static void FreeExtraFolders(); static void SetExtraIcons(char *name, int *id); static BOOL TryAddExtraFolderAndChildren(int parent_index); static BOOL TrySaveExtraFolder(LPTREEFOLDER lpFolder); static bool LoadCacheFolder(int parent_index, int id); static void SaveCacheFolder(int parent_index, const char *fname); /*************************************************************************** public functions ***************************************************************************/ BOOL win_move_file_utf8(const char *existingfilename, const char *newfilename) { BOOL result = false; TCHAR *t_existingfilename = ui_wstring_from_utf8(existingfilename); if( !t_existingfilename ) return result; TCHAR *t_newfilename = ui_wstring_from_utf8(newfilename); if( !t_newfilename ) { free(t_existingfilename); return result; } result = MoveFile(t_existingfilename, t_newfilename); free(t_newfilename); free(t_existingfilename); return result; } /************************************************************************** * ci_strncmp - case insensitive character array compare * * Returns zero if the first n characters of s1 and s2 are equal, * ignoring case. * stolen from datafile.c **************************************************************************/ static int ci_strncmp (const char *s1, const char *s2, int n) { int c1 = 0, c2 = 0; while (n) { if ((c1 = tolower (*s1)) != (c2 = tolower (*s2))) return (c1 - c2); else if (!c1) break; --n; s1++; s2++; } return 0; } /* De-allocate all folder memory */ void FreeFolders() { if (m_treeFolders) { if (m_numExtraFolders) { FreeExtraFolders(); m_numFolders -= m_numExtraFolders; } for (int i = m_numFolders - 1; i >= 0; i--) { DeleteFolder(m_treeFolders[i]); m_treeFolders[i] = NULL; m_numFolders--; } free(m_treeFolders); m_treeFolders = NULL; } m_numFolders = 0; } /* Reset folder filters */ void ResetFilters() { if (m_treeFolders) for (int i = 0; i < (int)m_numFolders; i++) m_treeFolders[i]->m_dwFlags &= ~FI_MASK; } int InitTree(LPCFOLDERDATA lpFolderData, LPCFILTER_ITEM lpFilterList) { m_lpFolderData = lpFolderData; m_lpFilterList = lpFilterList; InitFolders(); /* this will subclass the treeview (where WM_DRAWITEM gets sent for the header control) */ LONG_PTR l = GetWindowLongPtr(GetTreeView(), GWLP_WNDPROC); m_lpTreeWndProc = (WNDPROC)l; SetWindowLongPtr(GetTreeView(), GWLP_WNDPROC, (LONG_PTR)TreeWndProc); return m_numInbuiltFolders; } void SetCurrentFolder(LPTREEFOLDER lpFolder) { m_lpCurrentFolder = (lpFolder == 0) ? m_treeFolders[0] : lpFolder; m_nCurrentFolder = (m_lpCurrentFolder) ? m_lpCurrentFolder->m_nFolderId : 0; } LPTREEFOLDER GetCurrentFolder() { return m_lpCurrentFolder; } UINT GetCurrentFolderID() { return m_nCurrentFolder; } int GetNumFolders() { return m_numFolders; } LPTREEFOLDER GetFolder(UINT nFolder) { return (nFolder < m_numFolders) ? m_treeFolders[nFolder] : NULL; } LPTREEFOLDER GetFolderByID(UINT nID) { for (UINT i = 0; i < m_numFolders; i++) if (m_treeFolders[i]->m_nFolderId == nID) return m_treeFolders[i]; return (LPTREEFOLDER)0; } void AddGame(LPTREEFOLDER lpFolder, UINT nGame) { if (lpFolder) SetBit(lpFolder->m_lpGameBits, nGame); } void RemoveGame(LPTREEFOLDER lpFolder, UINT nGame) { ClearBit(lpFolder->m_lpGameBits, nGame); } int FindGame(LPTREEFOLDER lpFolder, int nGame) { return FindBit(lpFolder->m_lpGameBits, nGame, true); } // Called to re-associate games with folders void ResetWhichGamesInFolders() { int nGames = driver_list::total(); for (UINT i = 0; i < m_numFolders; i++) { LPTREEFOLDER lpFolder = m_treeFolders[i]; // setup the games in our built-in folders for (UINT k = 0; k < m_numInbuiltFolders; k++) { if (lpFolder->m_nFolderId == m_lpFolderData[k].m_nFolderId) { if (m_lpFolderData[k].m_pfnQuery || m_lpFolderData[k].m_bExpectedResult) { SetAllBits(lpFolder->m_lpGameBits, false); for (UINT jj = 0; jj < nGames; jj++) { // invoke the query function BOOL b = m_lpFolderData[k].m_pfnQuery ? m_lpFolderData[k].m_pfnQuery(jj) : true; // if we expect false, flip the result if (!m_lpFolderData[k].m_bExpectedResult) b = !b; // if we like what we hear, add the game if (b) AddGame(lpFolder, jj); } } break; } } } } /* Used to build the GameList */ BOOL GameFiltered(int nGame, DWORD dwMask) { LPTREEFOLDER lpFolder = GetCurrentFolder(); LPTREEFOLDER lpParent = NULL; //Filter out the Bioses on all Folders, except for the Bios Folder if(lpFolder && lpFolder->m_nFolderId != FOLDER_BIOS) { if(DriverIsBios(nGame)) return true; } if(driver_list::driver(nGame).name[0] == '_') return true; // Filter games--return true if the game should be HIDDEN in this view if( GetFilterInherit() ) { if( lpFolder ) { lpParent = GetFolder( lpFolder->m_nParent ); if( lpParent ) { /* Check the Parent Filters and inherit them on child, * The inherited filters don't display on the custom Filter Dialog for the Child folder * No need to promote all games to parent folder, works as is */ dwMask |= lpParent->m_dwFlags; } } } if (strlen(GetSearchText()) && _stricmp(GetSearchText(), SEARCH_PROMPT)) if (MyStrStrI(driver_list::driver(nGame).type.fullname(),GetSearchText()) == NULL && MyStrStrI(driver_list::driver(nGame).name,GetSearchText()) == NULL && MyStrStrI(driver_list::driver(nGame).manufacturer,GetSearchText()) == NULL) return true; /*Filter Text is already global*/ if (MyStrStrI(driver_list::driver(nGame).type.fullname(),GetFilterText()) == NULL && MyStrStrI(driver_list::driver(nGame).name,GetFilterText()) == NULL && MyStrStrI(driver_list::driver(nGame).type.source(),GetFilterText()) == NULL && MyStrStrI(driver_list::driver(nGame).manufacturer,GetFilterText()) == NULL) { return true; } // Are there filters set on this folder? if ((dwMask & FI_MASK) == 0) return false; // Filter out clones? if (dwMask & FI_CLONES && DriverIsClone(nGame)) return true; for (int i = 0; m_lpFilterList[i].m_dwFilterType; i++) if (dwMask & m_lpFilterList[i].m_dwFilterType) if (m_lpFilterList[i].m_pfnQuery(nGame) == m_lpFilterList[i].m_bExpectedResult) return true; return false; } /* Get the parent of game in this view */ BOOL GetParentFound(int nGame) // not used { LPTREEFOLDER lpFolder = GetCurrentFolder(); if( lpFolder ) { int nParentIndex = GetParentIndex(&driver_list::driver(nGame)); /* return false if no parent is there in this view */ if( nParentIndex == -1) return false; /* return false if the folder should be HIDDEN in this view */ if (TestBit(lpFolder->m_lpGameBits, nParentIndex) == 0) return false; /* return false if the game should be HIDDEN in this view */ if (GameFiltered(nParentIndex, lpFolder->m_dwFlags)) return false; return true; } return false; } LPCFILTER_ITEM GetFilterList() { return m_lpFilterList; } /*************************************************************************** private functions ***************************************************************************/ void CreateSaveStateFolders(int parent_index) { LPTREEFOLDER lpFolder = m_treeFolders[parent_index]; LPTREEFOLDER lpSupported, lpUnsupported; // no games in top level folder SetAllBits(lpFolder->m_lpGameBits,false); // create our two subfolders lpSupported = NewFolder("Supported", m_next_folder_id++, parent_index, IDI_FP_SAVESTATE, GetFolderFlags(m_numFolders)); lpUnsupported = NewFolder("Unsupported", m_next_folder_id++, parent_index, IDI_FP_SAVESTATE, GetFolderFlags(m_numFolders)); AddFolder(lpSupported); AddFolder(lpUnsupported); for (int jj = 0; jj < driver_list::total(); jj++) { if (DriverSupportsSaveState(jj)) AddGame(lpSupported, jj); else AddGame(lpUnsupported, jj); } } void CreateSourceFolders(int parent_index) { printf("CreateSourceFolders: begin\n");fflush(stdout); int i, k=0; int nGames = driver_list::total(); int start_folder = m_numFolders; LPTREEFOLDER lpFolder = m_treeFolders[parent_index]; // no games in top level folder SetAllBits(lpFolder->m_lpGameBits,false); for (int jj = 0; jj < nGames; jj++) { const char *s = GetDriverFilename(jj); if (s == NULL || s[0] == '\0') continue; // look for an existant source treefolder for this game // (likely to be the previous one, so start at the end) for (i=m_numFolders-1;i>=start_folder;i--) { if (strcmp(m_treeFolders[i]->m_lpTitle,s) == 0) { AddGame(m_treeFolders[i], jj); break; } } if (i == start_folder-1) { // nope, it's a source file we haven't seen before, make it. LPTREEFOLDER lpTemp = NewFolder(s, m_next_folder_id, parent_index, IDI_FC_SOURCE, GetFolderFlags(m_numFolders)); if (!lpTemp) continue; m_ExtraFolderData[m_next_folder_id] = (EXFOLDERDATA*)malloc(m_folderBytes); if (!m_ExtraFolderData[m_next_folder_id]) continue; memset(m_ExtraFolderData[m_next_folder_id], 0, m_folderBytes); m_ExtraFolderData[m_next_folder_id]->m_nFolderId = m_next_folder_id; m_ExtraFolderData[m_next_folder_id]->m_nIconId = IDI_FC_SOURCE; m_ExtraFolderData[m_next_folder_id]->m_nParent = lpFolder->m_nFolderId; m_ExtraFolderData[m_next_folder_id]->m_nSubIconId = -1; snprintf(m_ExtraFolderData[m_next_folder_id]->m_szTitle, std::size(m_ExtraFolderData[m_next_folder_id]->m_szTitle), "%s", s); m_ExtraFolderData[m_next_folder_id]->m_dwFlags = 0; // Increment next_folder_id here in case code is added above m_next_folder_id++; AddFolder(lpTemp); AddGame(lpTemp, jj); } } SetNumOptionFolders(k-1); printf("CreateSourceFolders: end\n"); fflush(stdout); } void CreateScreenFolders(int parent_index) { printf("CreateScreenFolders: begin\n");fflush(stdout); int i, k=0; int nGames = driver_list::total(); int start_folder = m_numFolders; LPTREEFOLDER lpFolder = m_treeFolders[parent_index]; // no games in top level folder SetAllBits(lpFolder->m_lpGameBits,false); for (int jj = 0; jj < nGames; jj++) { int screens = DriverNumScreens(jj); char s[2]{}; itoa(screens, s, 10); // look for an existant screens treefolder for this game // (likely to be the previous one, so start at the end) for (i=m_numFolders-1;i>=start_folder;i--) { if (strcmp(m_treeFolders[i]->m_lpTitle,s) == 0) { AddGame(m_treeFolders[i], jj); break; } } if (i == start_folder-1) { // nope, it's a screen file we haven't seen before, make it. LPTREEFOLDER lpTemp = NewFolder(s, m_next_folder_id, parent_index, IDI_FC_MONITOR, GetFolderFlags(m_numFolders)); if (!lpTemp) continue; m_ExtraFolderData[m_next_folder_id] = (EXFOLDERDATA*)malloc(m_folderBytes); if (!m_ExtraFolderData[m_next_folder_id]) continue; memset(m_ExtraFolderData[m_next_folder_id], 0, m_folderBytes); m_ExtraFolderData[m_next_folder_id]->m_nFolderId = m_next_folder_id; m_ExtraFolderData[m_next_folder_id]->m_nIconId = IDI_FC_MONITOR; m_ExtraFolderData[m_next_folder_id]->m_nParent = lpFolder->m_nFolderId; m_ExtraFolderData[m_next_folder_id]->m_nSubIconId = -1; snprintf(m_ExtraFolderData[m_next_folder_id]->m_szTitle, std::size(m_ExtraFolderData[m_next_folder_id]->m_szTitle), "%s", s); m_ExtraFolderData[m_next_folder_id]->m_dwFlags = 0; // Increment next_folder_id here in case code is added above m_next_folder_id++; AddFolder(lpTemp); AddGame(lpTemp, jj); } } SetNumOptionFolders(k-1); printf("CreateScreenFolders: end\n"); fflush(stdout); } void CreateManufacturerFolders(int parent_index) { printf("CreateManufacturerFolders: begin\n");fflush(stdout); int i; int nGames = driver_list::total(); int start_folder = m_numFolders; LPTREEFOLDER lpFolder = m_treeFolders[parent_index]; // no games in top level folder SetAllBits(lpFolder->m_lpGameBits,false); for (int jj = 0; jj < nGames; jj++) { string t = driver_list::driver(jj).manufacturer; const char ch = '.'; t.erase(remove(t.begin(), t.end(), ch), t.end()); // remove all periods const char *manufacturer = t.c_str(); int iChars = 0; while( manufacturer != NULL && manufacturer[0] != '\0' ) { const char *s = ParseManufacturer(manufacturer, &iChars); manufacturer += iChars; //shift to next start char if( s != NULL && *s != 0 ) { for (i=m_numFolders-1;i>=start_folder;i--) { //RS Made it case insensitive if (ci_strncmp(m_treeFolders[i]->m_lpTitle,s,20) == 0 ) { AddGame(m_treeFolders[i],jj); break; } } if (i == start_folder-1) { // nope, it's a manufacturer we haven't seen before, make it. LPTREEFOLDER lpTemp = NewFolder(s, m_next_folder_id, parent_index, IDI_FC_MANU, GetFolderFlags(m_numFolders)); if (!lpTemp) continue; m_ExtraFolderData[m_next_folder_id] = (EXFOLDERDATA*)malloc(m_folderBytes); if (!m_ExtraFolderData[m_next_folder_id]) continue; memset(m_ExtraFolderData[m_next_folder_id], 0, m_folderBytes); m_ExtraFolderData[m_next_folder_id]->m_nFolderId = m_next_folder_id; m_ExtraFolderData[m_next_folder_id]->m_nIconId = IDI_FC_MANU; m_ExtraFolderData[m_next_folder_id]->m_nParent = lpFolder->m_nFolderId; m_ExtraFolderData[m_next_folder_id]->m_nSubIconId = -1; snprintf(m_ExtraFolderData[m_next_folder_id]->m_szTitle, std::size(m_ExtraFolderData[m_next_folder_id]->m_szTitle), "%s", s); m_ExtraFolderData[m_next_folder_id++]->m_dwFlags = 0; AddFolder(lpTemp); AddGame(lpTemp,jj); } } } } printf("CreateManufacturerFolders: end\n"); fflush(stdout); } /* Make a reasonable name out of the one found in the driver array */ static const char *ParseManufacturer(const char *s, int *pParsedChars ) { static char tmp[256]{}; char *ptmp; char *t; *pParsedChars= 0; if ( *s == '?' || *s == '<' || s[3] == '?' ) { (*pParsedChars) = strlen(s); return ""; } ptmp = tmp; /*if first char is a space, skip it*/ if( *s == ' ' ) { (*pParsedChars)++; ++s; } while( *s ) { /* combinations where to end string */ if ( ( (*s == ' ') && ( s[1] == '(' || s[1] == '/' || s[1] == '+' ) ) || ( *s == ']' ) || ( *s == '/' ) || ( *s == '?' ) ) { (*pParsedChars)++; if( s[1] == '/' || s[1] == '+' ) (*pParsedChars)++; break; } if( s[0] == ' ' && s[1] == '?' ) { (*pParsedChars) += 2; s+=2; } /* skip over opening braces*/ if ( *s != '[' ) { *ptmp++ = *s; } (*pParsedChars)++; /*for "distributed by" and "supported by" handling*/ if( ( (s[1] == ',') && (s[2] == ' ') && ( (s[3] == 's') || (s[3] == 'd') ) ) ) { //*ptmp++ = *s; ++s; break; } ++s; } *ptmp = '\0'; t = tmp; if( tmp[0] == '(' || tmp[strlen(tmp)-1] == ')' || tmp[0] == ',') { ptmp = strchr( tmp,'(' ); if ( ptmp == NULL ) { ptmp = strchr( tmp,',' ); if( ptmp != NULL) { //parse the new "supported by" and "distributed by" ptmp++; if (ci_strncmp(ptmp, " supported by", 13) == 0) { ptmp += 13; } else if (ci_strncmp(ptmp, " distributed by", 15) == 0) { ptmp += 15; } else { return NULL; } } else { ptmp = tmp; if ( ptmp == NULL ) { return NULL; } } } if( tmp[0] == '(' || tmp[0] == ',') { ptmp++; } if (ci_strncmp(ptmp, "licensed from ", 14) == 0) { ptmp += 14; } // for the licenced from case if (ci_strncmp(ptmp, "licenced from ", 14) == 0) { ptmp += 14; } while ( (*ptmp != ')' ) && (*ptmp != '/' ) && *ptmp != '\0') { if (*ptmp == ' ' && ci_strncmp(ptmp, " license", 8) == 0) { break; } if (*ptmp == ' ' && ci_strncmp(ptmp, " licence", 8) == 0) { break; } *t++ = *ptmp++; } *t = '\0'; } *ptmp = '\0'; return tmp; } // A Bios folder contains the games that use that bios. // MESSUI only: has no games that need a bios, all Bioses use software lists instead. void CreateBIOSFolders(int parent_index) { printf("CreateBIOSFolders: begin\n");fflush(stdout); int i, nGames = driver_list::total(); int start_folder = m_numFolders; const game_driver *drv; int nParentIndex = -1; LPTREEFOLDER lpFolder = m_treeFolders[parent_index]; // no games in top level folder SetAllBits(lpFolder->m_lpGameBits, false); for (int jj = 0; jj < nGames; jj++) { if ( DriverIsClone(jj) ) { nParentIndex = GetParentIndex(&driver_list::driver(jj)); if (nParentIndex < 0) return; drv = &driver_list::driver(nParentIndex); } else drv = &driver_list::driver(jj); nParentIndex = GetParentIndex(drv); if (nParentIndex < 0 || !driver_list::driver(nParentIndex).type.fullname()) continue; for (i = m_numFolders-1; i >= start_folder; i--) { if (strcmp(m_treeFolders[i]->m_lpTitle, driver_list::driver(nParentIndex).type.fullname()) == 0) { AddGame(m_treeFolders[i], jj); break; } } if (i == start_folder-1) { LPTREEFOLDER lpTemp = NewFolder(driver_list::driver(nParentIndex).type.fullname(), m_next_folder_id++, parent_index, IDI_FC_BIOS, GetFolderFlags(m_numFolders)); if (lpTemp) { AddFolder(lpTemp); AddGame(lpTemp, jj); } } } printf("CreateBIOSFolders: end\n"); fflush(stdout); } void CreateCPUFolders(int parent_index) { printf("CreateCPUFolders: begin\n");fflush(stdout); int device_folder_count = 0; LPTREEFOLDER device_folders[1024]{}; LPTREEFOLDER folder; int nFolder = m_numFolders; for (int i = 0; i < driver_list::total(); i++) { machine_config config(driver_list::driver(i),MameUIGlobal()); // enumerate through all devices for (device_execute_interface &device : execute_interface_enumerator(config.root_device())) { // get the name const char* dev_name = device.device().name(); if (dev_name) // skip null { // do we have a folder for this device? folder = NULL; for (int j = 0; j < device_folder_count; j++) { if (strcmp(dev_name, device_folders[j]->m_lpTitle)==0) { folder = device_folders[j]; break; } } // are we forced to create a folder? if (folder == NULL) { LPTREEFOLDER lpTemp = NewFolder(dev_name, m_next_folder_id, parent_index, IDI_FC_CPU, GetFolderFlags(m_numFolders)); if (!lpTemp) continue; m_ExtraFolderData[m_next_folder_id] = (EXFOLDERDATA*)malloc(m_folderBytes); if (!m_ExtraFolderData[m_next_folder_id]) continue; memset(m_ExtraFolderData[m_next_folder_id], 0, m_folderBytes); m_ExtraFolderData[m_next_folder_id]->m_nFolderId = m_next_folder_id; m_ExtraFolderData[m_next_folder_id]->m_nIconId = IDI_FC_CPU; m_ExtraFolderData[m_next_folder_id]->m_nParent = m_treeFolders[parent_index]->m_nFolderId; m_ExtraFolderData[m_next_folder_id]->m_nSubIconId = -1; snprintf(m_ExtraFolderData[m_next_folder_id]->m_szTitle, std::size(m_ExtraFolderData[m_next_folder_id]->m_szTitle), "%s", dev_name); m_ExtraFolderData[m_next_folder_id++]->m_dwFlags = 0; AddFolder(lpTemp); folder = m_treeFolders[nFolder++]; // record that we found this folder device_folders[device_folder_count++] = folder; if (device_folder_count >= std::size(device_folders)) { printf("CreateCPUFolders: **buffer overrun**: %d, folder truncated\n",device_folder_count); fflush(stdout); return; } } // cpu type #'s are one-based AddGame(folder, i); } } } printf("CreateCPUFolders: used %d of %d available\n",device_folder_count,(int)std::size(device_folders)); fflush(stdout); } void CreateSoundFolders(int parent_index) { printf("CreateSoundFolders: begin\n");fflush(stdout); int device_folder_count = 0; LPTREEFOLDER device_folders[600]{}; LPTREEFOLDER folder; int nFolder = m_numFolders; for (int i = 0; i < driver_list::total(); i++) { machine_config config(driver_list::driver(i),MameUIGlobal()); // enumerate through all devices for (device_sound_interface &device : sound_interface_enumerator(config.root_device())) { // get the name const char* dev_name = device.device().name(); // do we have a folder for this device? if (dev_name) { folder = NULL; for (int j = 0; j < device_folder_count; j++) { if (strcmp(dev_name, device_folders[j]->m_lpTitle)==0) { folder = device_folders[j]; break; } } // are we forced to create a folder? if (folder == NULL) { LPTREEFOLDER lpTemp = NewFolder(dev_name, m_next_folder_id, parent_index, IDI_FC_SOUND, GetFolderFlags(m_numFolders)); if (!lpTemp) continue; m_ExtraFolderData[m_next_folder_id] = (EXFOLDERDATA*)malloc(m_folderBytes); if (!m_ExtraFolderData[m_next_folder_id]) continue; memset(m_ExtraFolderData[m_next_folder_id], 0, m_folderBytes); m_ExtraFolderData[m_next_folder_id]->m_nFolderId = m_next_folder_id; m_ExtraFolderData[m_next_folder_id]->m_nIconId = IDI_FC_SOUND; m_ExtraFolderData[m_next_folder_id]->m_nParent = m_treeFolders[parent_index]->m_nFolderId; m_ExtraFolderData[m_next_folder_id]->m_nSubIconId = -1; snprintf(m_ExtraFolderData[m_next_folder_id]->m_szTitle, std::size(m_ExtraFolderData[m_next_folder_id]->m_szTitle), "%s", dev_name); m_ExtraFolderData[m_next_folder_id++]->m_dwFlags = 0; AddFolder(lpTemp); folder = m_treeFolders[nFolder++]; // record that we found this folder device_folders[device_folder_count++] = folder; if (device_folder_count >= std::size(device_folders)) { printf("CreateSoundFolders: **buffer overrun**: %d, folder truncated\n",device_folder_count); fflush(stdout); return; } } // cpu type #'s are one-based AddGame(folder, i); } } } printf("CreateSoundFolders: used %d of %d available\n",device_folder_count,(int)std::size(device_folders)); fflush(stdout); } void CreateDeficiencyFolders(int parent_index) { printf("CreateDeficiencyFolders: begin\n");fflush(stdout); int nGames = driver_list::total(); LPTREEFOLDER lpFolder = m_treeFolders[parent_index]; // create our subfolders LPTREEFOLDER lpProt, lpWrongCol, lpImpCol, lpImpGraph, lpMissSnd, lpImpSnd, lpFlip, lpArt; lpProt = NewFolder("Unemulated Protection", m_next_folder_id, parent_index, IDI_FP_IMP, GetFolderFlags(m_numFolders)); if (!lpProt) return; m_ExtraFolderData[m_next_folder_id] = (EXFOLDERDATA*)malloc(m_folderBytes); if (!m_ExtraFolderData[m_next_folder_id]) return; memset(m_ExtraFolderData[m_next_folder_id], 0, m_folderBytes); m_ExtraFolderData[m_next_folder_id]->m_nFolderId = m_next_folder_id; m_ExtraFolderData[m_next_folder_id]->m_nIconId = IDI_FP_IMP; m_ExtraFolderData[m_next_folder_id]->m_nParent = lpFolder->m_nFolderId; m_ExtraFolderData[m_next_folder_id]->m_nSubIconId = -1; snprintf(m_ExtraFolderData[m_next_folder_id]->m_szTitle, std::size(m_ExtraFolderData[m_next_folder_id]->m_szTitle), "Unemulated Protection"); m_ExtraFolderData[m_next_folder_id++]->m_dwFlags = 0; AddFolder(lpProt); lpWrongCol = NewFolder("Wrong Colours", m_next_folder_id, parent_index, IDI_FP_IMP, GetFolderFlags(m_numFolders)); if (!lpWrongCol) return; m_ExtraFolderData[m_next_folder_id] = (EXFOLDERDATA*)malloc(m_folderBytes); if (!m_ExtraFolderData[m_next_folder_id]) return; memset(m_ExtraFolderData[m_next_folder_id], 0, m_folderBytes); m_ExtraFolderData[m_next_folder_id]->m_nFolderId = m_next_folder_id; m_ExtraFolderData[m_next_folder_id]->m_nIconId = IDI_FP_IMP; m_ExtraFolderData[m_next_folder_id]->m_nParent = lpFolder->m_nFolderId; m_ExtraFolderData[m_next_folder_id]->m_nSubIconId = -1; snprintf(m_ExtraFolderData[m_next_folder_id]->m_szTitle, std::size(m_ExtraFolderData[m_next_folder_id]->m_szTitle), "Wrong Colours"); m_ExtraFolderData[m_next_folder_id++]->m_dwFlags = 0; AddFolder(lpWrongCol); lpImpCol = NewFolder("Imperfect Colours", m_next_folder_id, parent_index, IDI_FP_IMP, GetFolderFlags(m_numFolders)); if (!lpImpCol) return; m_ExtraFolderData[m_next_folder_id] = (EXFOLDERDATA*)malloc(m_folderBytes); if (!m_ExtraFolderData[m_next_folder_id]) return; memset(m_ExtraFolderData[m_next_folder_id], 0, m_folderBytes); m_ExtraFolderData[m_next_folder_id]->m_nFolderId = m_next_folder_id; m_ExtraFolderData[m_next_folder_id]->m_nIconId = IDI_FP_IMP; m_ExtraFolderData[m_next_folder_id]->m_nParent = lpFolder->m_nFolderId; m_ExtraFolderData[m_next_folder_id]->m_nSubIconId = -1; snprintf(m_ExtraFolderData[m_next_folder_id]->m_szTitle, std::size(m_ExtraFolderData[m_next_folder_id]->m_szTitle), "Imperfect Colours"); m_ExtraFolderData[m_next_folder_id++]->m_dwFlags = 0; AddFolder(lpImpCol); lpImpGraph = NewFolder("Imperfect Graphics", m_next_folder_id, parent_index, IDI_FP_IMP, GetFolderFlags(m_numFolders)); if (!lpImpGraph) return; m_ExtraFolderData[m_next_folder_id] = (EXFOLDERDATA*)malloc(m_folderBytes); if (!m_ExtraFolderData[m_next_folder_id]) return; memset(m_ExtraFolderData[m_next_folder_id], 0, m_folderBytes); m_ExtraFolderData[m_next_folder_id]->m_nFolderId = m_next_folder_id; m_ExtraFolderData[m_next_folder_id]->m_nIconId = IDI_FP_IMP; m_ExtraFolderData[m_next_folder_id]->m_nParent = lpFolder->m_nFolderId; m_ExtraFolderData[m_next_folder_id]->m_nSubIconId = -1; snprintf(m_ExtraFolderData[m_next_folder_id]->m_szTitle, std::size(m_ExtraFolderData[m_next_folder_id]->m_szTitle), "Imperfect Graphics"); m_ExtraFolderData[m_next_folder_id++]->m_dwFlags = 0; AddFolder(lpImpGraph); lpMissSnd = NewFolder("Missing Sound", m_next_folder_id, parent_index, IDI_FP_SOUND, GetFolderFlags(m_numFolders)); if (!lpMissSnd) return; m_ExtraFolderData[m_next_folder_id] = (EXFOLDERDATA*)malloc(m_folderBytes); if (!m_ExtraFolderData[m_next_folder_id]) return; memset(m_ExtraFolderData[m_next_folder_id], 0, m_folderBytes); m_ExtraFolderData[m_next_folder_id]->m_nFolderId = m_next_folder_id; m_ExtraFolderData[m_next_folder_id]->m_nIconId = IDI_FP_SOUND; m_ExtraFolderData[m_next_folder_id]->m_nParent = lpFolder->m_nFolderId; m_ExtraFolderData[m_next_folder_id]->m_nSubIconId = -1; snprintf(m_ExtraFolderData[m_next_folder_id]->m_szTitle, std::size(m_ExtraFolderData[m_next_folder_id]->m_szTitle), "Missing Sound"); m_ExtraFolderData[m_next_folder_id++]->m_dwFlags = 0; AddFolder(lpMissSnd); lpImpSnd = NewFolder("Imperfect Sound", m_next_folder_id, parent_index, IDI_FP_SOUND, GetFolderFlags(m_numFolders)); if (!lpImpSnd) return; m_ExtraFolderData[m_next_folder_id] = (EXFOLDERDATA*)malloc(m_folderBytes); if (!m_ExtraFolderData[m_next_folder_id]) return; memset(m_ExtraFolderData[m_next_folder_id], 0, m_folderBytes); m_ExtraFolderData[m_next_folder_id]->m_nFolderId = m_next_folder_id; m_ExtraFolderData[m_next_folder_id]->m_nIconId = IDI_FP_SOUND; m_ExtraFolderData[m_next_folder_id]->m_nParent = lpFolder->m_nFolderId; m_ExtraFolderData[m_next_folder_id]->m_nSubIconId = -1; snprintf(m_ExtraFolderData[m_next_folder_id]->m_szTitle, std::size(m_ExtraFolderData[m_next_folder_id]->m_szTitle), "Imperfect Sound"); m_ExtraFolderData[m_next_folder_id++]->m_dwFlags = 0; AddFolder(lpImpSnd); lpFlip = NewFolder("No Cocktail", m_next_folder_id, parent_index, IDI_FP_IMP, GetFolderFlags(m_numFolders)); if (!lpFlip) return; m_ExtraFolderData[m_next_folder_id] = (EXFOLDERDATA*)malloc(m_folderBytes); if (!m_ExtraFolderData[m_next_folder_id]) return; memset(m_ExtraFolderData[m_next_folder_id], 0, m_folderBytes); m_ExtraFolderData[m_next_folder_id]->m_nFolderId = m_next_folder_id; m_ExtraFolderData[m_next_folder_id]->m_nIconId = IDI_FP_IMP; m_ExtraFolderData[m_next_folder_id]->m_nParent = lpFolder->m_nFolderId; m_ExtraFolderData[m_next_folder_id]->m_nSubIconId = -1; snprintf(m_ExtraFolderData[m_next_folder_id]->m_szTitle, std::size(m_ExtraFolderData[m_next_folder_id]->m_szTitle), "No Cocktail"); m_ExtraFolderData[m_next_folder_id++]->m_dwFlags = 0; AddFolder(lpFlip); lpArt = NewFolder("Requires Artwork", m_next_folder_id, parent_index, IDI_FP_IMP, GetFolderFlags(m_numFolders)); if (!lpArt) return; m_ExtraFolderData[m_next_folder_id] = (EXFOLDERDATA*)malloc(m_folderBytes ); if (!m_ExtraFolderData[m_next_folder_id]) return; memset(m_ExtraFolderData[m_next_folder_id], 0, m_folderBytes); m_ExtraFolderData[m_next_folder_id]->m_nFolderId = m_next_folder_id; m_ExtraFolderData[m_next_folder_id]->m_nIconId = IDI_FP_IMP; m_ExtraFolderData[m_next_folder_id]->m_nParent = lpFolder->m_nFolderId; m_ExtraFolderData[m_next_folder_id]->m_nSubIconId = -1; snprintf(m_ExtraFolderData[m_next_folder_id]->m_szTitle, std::size(m_ExtraFolderData[m_next_folder_id]->m_szTitle), "Requires Artwork"); m_ExtraFolderData[m_next_folder_id++]->m_dwFlags = 0; AddFolder(lpArt); // no games in top level folder SetAllBits(lpFolder->m_lpGameBits,false); for (int jj = 0; jj < nGames; jj++) { uint32_t cache = GetDriverCacheLower(jj); if (BIT(cache, 21)) AddGame(lpWrongCol,jj); if (BIT(cache, 22)) AddGame(lpProt,jj); if (BIT(cache, 20)) AddGame(lpImpCol,jj); if (BIT(cache, 18)) AddGame(lpImpGraph,jj); if (BIT(cache, 17)) AddGame(lpMissSnd,jj); if (BIT(cache, 16)) AddGame(lpImpSnd,jj); if (BIT(cache, 8)) AddGame(lpFlip,jj); if (BIT(cache, 10)) AddGame(lpArt,jj); } printf("CreateDeficiencyFolders: end\n"); fflush(stdout); } void CreateDumpingFolders(int parent_index) { printf("CreateDumpingFolders: begin\n");fflush(stdout); BOOL bBadDump = false; BOOL bNoDump = false; int nGames = driver_list::total(); LPTREEFOLDER lpFolder = m_treeFolders[parent_index]; const rom_entry *rom; const game_driver *gamedrv; // create our two subfolders LPTREEFOLDER lpBad, lpNo; lpBad = NewFolder("Bad Dump", m_next_folder_id, parent_index, IDI_FP_DUMP, GetFolderFlags(m_numFolders)); if (!lpBad) return; m_ExtraFolderData[m_next_folder_id] = (EXFOLDERDATA*)malloc(m_folderBytes); if (!m_ExtraFolderData[m_next_folder_id]) return; memset(m_ExtraFolderData[m_next_folder_id], 0, m_folderBytes); m_ExtraFolderData[m_next_folder_id]->m_nFolderId = m_next_folder_id; m_ExtraFolderData[m_next_folder_id]->m_nIconId = IDI_FP_DUMP; m_ExtraFolderData[m_next_folder_id]->m_nParent = lpFolder->m_nFolderId; m_ExtraFolderData[m_next_folder_id]->m_nSubIconId = -1; snprintf(m_ExtraFolderData[m_next_folder_id]->m_szTitle, std::size(m_ExtraFolderData[m_next_folder_id]->m_szTitle), "Bad Dump"); m_ExtraFolderData[m_next_folder_id++]->m_dwFlags = 0; AddFolder(lpBad); lpNo = NewFolder("No Dump", m_next_folder_id, parent_index, IDI_FP_DUMP, GetFolderFlags(m_numFolders)); if (!lpNo) return; m_ExtraFolderData[m_next_folder_id] = (EXFOLDERDATA*)malloc(m_folderBytes); if (!m_ExtraFolderData[m_next_folder_id]) return; memset(m_ExtraFolderData[m_next_folder_id], 0, m_folderBytes); m_ExtraFolderData[m_next_folder_id]->m_nFolderId = m_next_folder_id; m_ExtraFolderData[m_next_folder_id]->m_nIconId = IDI_FP_DUMP; m_ExtraFolderData[m_next_folder_id]->m_nParent = lpFolder->m_nFolderId; m_ExtraFolderData[m_next_folder_id]->m_nSubIconId = -1; snprintf(m_ExtraFolderData[m_next_folder_id]->m_szTitle, std::size(m_ExtraFolderData[m_next_folder_id]->m_szTitle), "No Dump"); m_ExtraFolderData[m_next_folder_id++]->m_dwFlags = 0; AddFolder(lpNo); // no games in top level folder SetAllBits(lpFolder->m_lpGameBits,false); for (int jj = 0; jj < nGames; jj++) { gamedrv = &driver_list::driver(jj); if (!gamedrv->rom) continue; bBadDump = false; bNoDump = false; /* Allocate machine config */ machine_config config(*gamedrv,MameUIGlobal()); for (device_t &device : device_enumerator(config.root_device())) { for (const rom_entry *region = rom_first_region(device); region; region = rom_next_region(region)) { for (rom = rom_first_file(region); rom; rom = rom_next_file(rom)) { if (ROMREGION_ISROMDATA(region) || ROMREGION_ISDISKDATA(region) ) { //name = ROM_GETNAME(rom); util::hash_collection hashes(rom->hashdata()); if (hashes.flag(util::hash_collection::FLAG_BAD_DUMP)) bBadDump = true; if (hashes.flag(util::hash_collection::FLAG_NO_DUMP)) bNoDump = true; } } } } if (bBadDump) AddGame(lpBad,jj); if (bNoDump) AddGame(lpNo,jj); } printf("CreateDumpingFolders: end\n"); fflush(stdout); } void CreateYearFolders(int parent_index) { printf("CreateYearFolders: begin\n");fflush(stdout); int i,jj; int nGames = driver_list::total(); int start_folder = m_numFolders; LPTREEFOLDER lpFolder = m_treeFolders[parent_index]; // no games in top level folder SetAllBits(lpFolder->m_lpGameBits, false); for (jj = 0; jj < nGames; jj++) { char s[strlen(driver_list::driver(jj).year)+1]; strcpy(s,driver_list::driver(jj).year); if (s[0] == '\0' || s[0] == '?') continue; if (s[4] == '?') s[4] = '\0'; // look for an extant year treefolder for this game // (likely to be the previous one, so start at the end) for (i=m_numFolders-1;i>=start_folder;i--) { if (strncmp(m_treeFolders[i]->m_lpTitle, s, 4) == 0) { AddGame(m_treeFolders[i], jj); break; } } if (i == start_folder-1) { // nope, it's a year we haven't seen before, make it. LPTREEFOLDER lpTemp; lpTemp = NewFolder(s, m_next_folder_id, parent_index, IDI_FC_YEAR, GetFolderFlags(m_numFolders)); if (!lpTemp) continue; m_ExtraFolderData[m_next_folder_id] = (EXFOLDERDATA*)malloc(m_folderBytes); if (!m_ExtraFolderData[m_next_folder_id]) continue; memset(m_ExtraFolderData[m_next_folder_id], 0, m_folderBytes); m_ExtraFolderData[m_next_folder_id]->m_nFolderId = m_next_folder_id; m_ExtraFolderData[m_next_folder_id]->m_nIconId = IDI_FC_YEAR; m_ExtraFolderData[m_next_folder_id]->m_nParent = lpFolder->m_nFolderId; m_ExtraFolderData[m_next_folder_id]->m_nSubIconId = -1; snprintf(m_ExtraFolderData[m_next_folder_id]->m_szTitle, std::size(m_ExtraFolderData[m_next_folder_id]->m_szTitle), "%s", s); m_ExtraFolderData[m_next_folder_id++]->m_dwFlags = 0; AddFolder(lpTemp); AddGame(lpTemp, jj); } } printf("CreateYearFolders: end\n"); fflush(stdout); } void CreateResolutionFolders(int parent_index) { printf("CreateResolutionFolders: begin\n");fflush(stdout); int i,jj; int nGames = driver_list::total(); int start_folder = m_numFolders; char Screen[2048]{}; const game_driver *gamedrv; LPTREEFOLDER lpFolder = m_treeFolders[parent_index]; // no games in top level folder SetAllBits(lpFolder->m_lpGameBits, false); for (jj = 0; jj < nGames; jj++) { gamedrv = &driver_list::driver(jj); /* Allocate machine config */ machine_config config(*gamedrv,MameUIGlobal()); if (isDriverVector(&config)) strcpy(Screen, "Vector"); else { screen_device_enumerator iter(config.root_device()); const screen_device *screen = iter.first(); if (screen == NULL) strcpy(Screen, "Screenless Game"); else { for (screen_device &screen : screen_device_enumerator(config.root_device())) { const rectangle &visarea = screen.visible_area(); snprintf(Screen, std::size(Screen), "%d x %d (%c)", visarea.max_y - visarea.min_y + 1, visarea.max_x - visarea.min_x + 1, (driver_list::driver(jj).flags & ORIENTATION_SWAP_XY) ? 'V':'H'); // look for an existant screen treefolder for this game // (likely to be the previous one, so start at the end) for (i=m_numFolders-1;i>=start_folder;i--) { if (strcmp(m_treeFolders[i]->m_lpTitle, Screen) == 0) { AddGame(m_treeFolders[i],jj); break; } } if (i == start_folder-1) { // nope, it's a screen we haven't seen before, make it. LPTREEFOLDER lpTemp = NewFolder(Screen, m_next_folder_id++, parent_index, IDI_FC_MONITOR, GetFolderFlags(m_numFolders)); if (!lpTemp) continue; m_ExtraFolderData[m_next_folder_id] = (EXFOLDERDATA*)malloc(m_folderBytes); if (!m_ExtraFolderData[m_next_folder_id]) continue; memset(m_ExtraFolderData[m_next_folder_id], 0, m_folderBytes); m_ExtraFolderData[m_next_folder_id]->m_nFolderId = m_next_folder_id; m_ExtraFolderData[m_next_folder_id]->m_nIconId = IDI_FC_MONITOR; m_ExtraFolderData[m_next_folder_id]->m_nParent = lpFolder->m_nFolderId; m_ExtraFolderData[m_next_folder_id]->m_nSubIconId = -1; snprintf(m_ExtraFolderData[m_next_folder_id]->m_szTitle, std::size(m_ExtraFolderData[m_next_folder_id]->m_szTitle), "%s", Screen); m_ExtraFolderData[m_next_folder_id++]->m_dwFlags = 0; AddFolder(lpTemp); AddGame(lpTemp,jj); } } } } } printf("CreateResolutionFolders: end\n"); fflush(stdout); } void CreateFPSFolders(int parent_index) { printf("CreateFPSFolders: begin\n");fflush(stdout); int i,jj; int nGames = driver_list::total(); int start_folder = m_numFolders; char Screen[2048]{}; const game_driver *gamedrv; LPTREEFOLDER lpFolder = m_treeFolders[parent_index]; // no games in top level folder SetAllBits(lpFolder->m_lpGameBits, false); for (jj = 0; jj < nGames; jj++) { gamedrv = &driver_list::driver(jj); /* Allocate machine config */ machine_config config(*gamedrv,MameUIGlobal()); if (isDriverVector(&config)) strcpy(Screen, "Vector"); else { screen_device_enumerator iter(config.root_device()); const screen_device *screen = iter.first(); if (screen == NULL) strcpy(Screen, "Screenless Game"); else { for (screen_device &screen : screen_device_enumerator(config.root_device())) { snprintf(Screen, std::size(Screen), "%f Hz", screen.frame_period().as_hz()); // look for an existant screen treefolder for this game // (likely to be the previous one, so start at the end) for (i=m_numFolders-1;i>=start_folder;i--) { if (strcmp(m_treeFolders[i]->m_lpTitle, Screen) == 0) { AddGame(m_treeFolders[i],jj); break; } } if (i == start_folder-1) { // nope, it's a screen we haven't seen before, make it. LPTREEFOLDER lpTemp = NewFolder(Screen, m_next_folder_id++, parent_index, IDI_FP_FPS, GetFolderFlags(m_numFolders)); if (!lpTemp) continue; m_ExtraFolderData[m_next_folder_id] = (EXFOLDERDATA*)malloc(m_folderBytes); if (!m_ExtraFolderData[m_next_folder_id]) continue; memset(m_ExtraFolderData[m_next_folder_id], 0, m_folderBytes); m_ExtraFolderData[m_next_folder_id]->m_nFolderId = m_next_folder_id; m_ExtraFolderData[m_next_folder_id]->m_nIconId = IDI_FP_FPS; m_ExtraFolderData[m_next_folder_id]->m_nParent = lpFolder->m_nFolderId; m_ExtraFolderData[m_next_folder_id]->m_nSubIconId = -1; snprintf(m_ExtraFolderData[m_next_folder_id]->m_szTitle, std::size(m_ExtraFolderData[m_next_folder_id]->m_szTitle), "%s", Screen); m_ExtraFolderData[m_next_folder_id++]->m_dwFlags = 0; AddFolder(lpTemp); AddGame(lpTemp,jj); } } } } } printf("CreateFPSFolders: end\n"); fflush(stdout); } //#pragma GCC diagnostic ignored "-Wunused-but-set-variable" // adds these folders to the treeview void ResetTreeViewFolders() { HWND hTreeView = GetTreeView(); // currently "cached" parent HTREEITEM hti_parent = NULL; int index_parent = -1; (void)TreeView_DeleteAllItems(hTreeView); //printf("Adding folders to tree ui indices %i to %i\n",start_index,end_index); TVINSERTSTRUCT tvs; tvs.hInsertAfter = TVI_SORT; TVITEM tvi; for (int i=0; im_nParent == -1) { if (lpFolder->m_nFolderId < MAX_FOLDERS) // it's a built in folder, let's see if we should show it if (GetShowFolder(lpFolder->m_nFolderId) == false) continue; tvi.mask = TVIF_TEXT | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE; tvs.hParent = TVI_ROOT; tvi.pszText = lpFolder->m_lptTitle; tvi.lParam = (LPARAM)lpFolder; tvi.iImage = GetTreeViewIconIndex(lpFolder->m_nIconId); tvi.iSelectedImage = 0; tvs.item = tvi; // Add root branch hti_parent = TreeView_InsertItem(hTreeView, &tvs); continue; } // not a top level branch, so look for parent if (m_treeFolders[i]->m_nParent != index_parent) { hti_parent = TreeView_GetRoot(hTreeView); while (1) { if (hti_parent == NULL) // couldn't find parent folder, so it's a built-in but // not shown folder break; tvi.hItem = hti_parent; tvi.mask = TVIF_PARAM; (void)TreeView_GetItem(hTreeView,&tvi); if (((LPTREEFOLDER)tvi.lParam) == m_treeFolders[m_treeFolders[i]->m_nParent]) break; hti_parent = TreeView_GetNextSibling(hTreeView,hti_parent); } // if parent is not shown, then don't show the child either obviously! if (hti_parent == NULL) continue; index_parent = m_treeFolders[i]->m_nParent; } tvi.mask = TVIF_TEXT | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE; tvs.hParent = hti_parent; tvi.iImage = GetTreeViewIconIndex(m_treeFolders[i]->m_nIconId); tvi.iSelectedImage = 0; tvi.pszText = m_treeFolders[i]->m_lptTitle; tvi.lParam = (LPARAM)m_treeFolders[i]; tvs.item = tvi; // Add it to this tree branch (void)TreeView_InsertItem(hTreeView, &tvs); // for current child branches } } //#pragma GCC diagnostic error "-Wunused-but-set-variable" void SelectTreeViewFolder(int folder_id) { HWND hTreeView = GetTreeView(); HTREEITEM hti; TVITEM tvi; memset(&tvi,0,sizeof(tvi)); hti = TreeView_GetRoot(hTreeView); while (hti != NULL) { HTREEITEM hti_next; tvi.hItem = hti; tvi.mask = TVIF_PARAM; (void)TreeView_GetItem(hTreeView,&tvi); if (((LPTREEFOLDER)tvi.lParam)->m_nFolderId == folder_id) { (void)TreeView_SelectItem(hTreeView,tvi.hItem); SetCurrentFolder((LPTREEFOLDER)tvi.lParam); return; } hti_next = TreeView_GetChild(hTreeView,hti); if (hti_next == NULL) { hti_next = TreeView_GetNextSibling(hTreeView,hti); if (hti_next == NULL) { hti_next = TreeView_GetParent(hTreeView,hti); if (hti_next != NULL) hti_next = TreeView_GetNextSibling(hTreeView,hti_next); } } hti = hti_next; } // could not find folder to select // make sure we select something tvi.hItem = TreeView_GetRoot(hTreeView); tvi.mask = TVIF_PARAM; (void)TreeView_GetItem(hTreeView,&tvi); (void)TreeView_SelectItem(hTreeView,tvi.hItem); SetCurrentFolder((LPTREEFOLDER)tvi.lParam); } /* * Does this folder have an INI associated with it? * Currently only true for FOLDER_VECTOR and children * of FOLDER_SOURCE. */ static BOOL FolderHasIni(LPTREEFOLDER lpFolder) { LPCFOLDERDATA data = FindFilter(lpFolder->m_nFolderId); if (data) if (data->m_opttype < OPTIONS_MAX) return true; if (lpFolder->m_nParent != -1 && FOLDER_SOURCE == m_treeFolders[lpFolder->m_nParent]->m_nFolderId) return true; return false; } /* Add a folder to the list. Does not allocate */ static BOOL AddFolder(LPTREEFOLDER lpFolder) { if (!lpFolder) return false; TREEFOLDER **tmpTree = NULL; UINT oldFolderArrayLength = m_folderArrayLength; if (m_numFolders + 1 >= m_folderArrayLength) { m_folderArrayLength += 500; tmpTree = (TREEFOLDER **)malloc(sizeof(TREEFOLDER **) * m_folderArrayLength); if (tmpTree) { memcpy(tmpTree,m_treeFolders,sizeof(TREEFOLDER **) * oldFolderArrayLength); if (m_treeFolders) free(m_treeFolders); m_treeFolders = tmpTree; } } /* Is there an folder.ini that can be edited? */ if (FolderHasIni(lpFolder)) lpFolder->m_dwFlags |= FI_INIEDIT; m_treeFolders[m_numFolders] = lpFolder; m_numFolders++; return true; } /* Allocate and initialize a NEW TREEFOLDER */ static LPTREEFOLDER NewFolder(const char *lpTitle, UINT nFolderId, int nParent, UINT nIconId, DWORD dwFlags) { LPTREEFOLDER lpFolder = (LPTREEFOLDER)malloc(sizeof(TREEFOLDER)); if (!lpFolder) return NULL; memset(lpFolder, '\0', sizeof(TREEFOLDER)); snprintf(lpFolder->m_lpTitle, std::size(lpFolder->m_lpTitle), "%s", lpTitle); lpFolder->m_lptTitle = ui_wstring_from_utf8(lpFolder->m_lpTitle); lpFolder->m_lpGameBits = NewBits(driver_list::total()); lpFolder->m_nFolderId = nFolderId; lpFolder->m_nParent = nParent; lpFolder->m_nIconId = nIconId; lpFolder->m_dwFlags = dwFlags; return lpFolder; } /* Deallocate the passed in LPTREEFOLDER */ static void DeleteFolder(LPTREEFOLDER lpFolder) { if (lpFolder) { if (lpFolder->m_lpGameBits) { DeleteBits(lpFolder->m_lpGameBits); lpFolder->m_lpGameBits = 0; } free(lpFolder->m_lptTitle); lpFolder->m_lptTitle = 0; //free(lpFolder->m_lpTitle); //lpFolder->m_lpTitle = 0; free(lpFolder); lpFolder = 0; } } /* Can be called to re-initialize the array of treeFolders */ BOOL InitFolders() { int i = 0; m_numInbuiltFolders = 0; DWORD dwFolderFlags = 0; if (m_treeFolders != NULL) { for (i = m_numFolders - 1; i >= 0; i--) { DeleteFolder(m_treeFolders[i]); m_treeFolders[i] = 0; m_numFolders--; } } m_numFolders = 0; if (m_folderArrayLength == 0) { m_folderArrayLength = 200; m_treeFolders = (TREEFOLDER **)malloc(sizeof(TREEFOLDER **) * m_folderArrayLength); if (!m_treeFolders) { m_folderArrayLength = 0; return 0; } else memset(m_treeFolders,'\0', sizeof(TREEFOLDER **) * m_folderArrayLength); } // built-in top level folders, initialise count of folders for (i = 0; m_lpFolderData[i].short_name; i++) { if (RequiredDriverCache() || (!RequiredDriverCache() && !m_lpFolderData[i].m_process)) { LPCFOLDERDATA fData = &m_lpFolderData[i]; /* get the saved folder flags */ dwFolderFlags = GetFolderFlags(m_numFolders); /* create the folder */ //printf("creating %s top-level folder\n",fData->m_lpTitle);fflush(stdout); AddFolder(NewFolder(fData->m_lpTitle, fData->m_nFolderId, -1, fData->m_nIconId, dwFolderFlags)); } m_numInbuiltFolders++; } // cached top level folders for (i = 0; i < m_numInbuiltFolders; i++) { if (!RequiredDriverCache() && m_lpFolderData[i].m_process) { LPCFOLDERDATA fData = &m_lpFolderData[i]; /* get the saved folder flags */ dwFolderFlags = GetFolderFlags(m_numFolders); /* create the folder with parent-level icon */ AddFolder(NewFolder(fData->m_lpTitle, fData->m_nFolderId, -1, fData->m_nIconId, dwFolderFlags)); } } m_numExtraFolders = InitExtraFolders(); for (i = 0; i < m_numExtraFolders; i++) { LPEXFOLDERDATA fExData = m_ExtraFolderData[i]; // OR in the saved folder flags dwFolderFlags = fExData->m_dwFlags | GetFolderFlags(m_numFolders); // Custom folder must not have the same name as an inbuilt one int k = 0; for (int j = 0; j < m_numInbuiltFolders; j++) if (strcmp(fExData->m_szTitle, m_lpFolderData[j].m_lpTitle)==0) k++; if (k == 0) { //printf("Creating custom folder: %s with iconid %d\n",fExData->m_szTitle,fExData->m_nIconId); AddFolder(NewFolder(fExData->m_szTitle,fExData->m_nFolderId,fExData->m_nParent, fExData->m_nIconId,dwFolderFlags)); } } // creates child folders of all the top level folders, including custom ones int num_top_level_folders = m_numFolders; for (int i = 0; i < num_top_level_folders; i++) { LPTREEFOLDER lpFolder = m_treeFolders[i]; LPCFOLDERDATA lpFolderData = NULL; for (int j = 0; j < m_numInbuiltFolders; j++) { if (m_lpFolderData[j].m_nFolderId == lpFolder->m_nFolderId) { lpFolderData = &m_lpFolderData[j]; break; } } if (lpFolderData) { if (lpFolderData->m_pfnCreateFolders) { if (RequiredDriverCache() && lpFolderData->m_process) // rebuild cache { lpFolderData->m_pfnCreateFolders(i); // create SaveCacheFolder(i, m_lpFolderData[i].short_name); // save } else if (!RequiredDriverCache() && lpFolderData->m_process) // reload cache { UINT ico2 = lpFolderData->m_nIconId2; if (ico2 == 0) ico2 = lpFolderData->m_nIconId; if (!LoadCacheFolder(i, ico2)) // load cache file { lpFolderData->m_pfnCreateFolders(i); // if it was missing, make a new one SaveCacheFolder(i, m_lpFolderData[i].short_name); // and save it } } else if (!lpFolderData->m_process) // build every time (CreateDeficiencyFolders) lpFolderData->m_pfnCreateFolders(i); // create but do not save } } else { if ((lpFolder->m_dwFlags & FI_CUSTOM) == 0) { printf("Internal inconsistency with non-built-in folder, but not custom\n"); continue; } //printf("Loading custom folder %i %i\n",i,lpFolder->m_nFolderId); // load the extra folder files, which also adds children if (TryAddExtraFolderAndChildren(i) == false) lpFolder->m_nFolderId = FOLDER_NONE; } } CreateTreeIcons(); ResetWhichGamesInFolders(); ResetTreeViewFolders(); SelectTreeViewFolder(GetSavedFolderID()); LoadFolderFlags(); return true; } // This shows the tree icons. If it fails, then we get a W2K folder structure. static BOOL CreateTreeIcons() { HICON hIcon; INT i; HINSTANCE hInst = GetModuleHandle(0); int numIcons = ICON_MAX + m_numExtraIcons; m_hTreeSmall = ImageList_Create (16, 16, ILC_COLORDDB | ILC_MASK, numIcons, numIcons); // Icons for inbuilt folders //printf("Trying to load %i normal icons\n",ICON_MAX); for (i = 0; i < ICON_MAX; i++) { // Try an internal icon hIcon = LoadIconFromFile(treeIconNames[i].lpName); if (!hIcon) hIcon = LoadIcon(hInst, MAKEINTRESOURCE(treeIconNames[i].nResourceID)); // Add icon to imagelist if (ImageList_AddIcon (m_hTreeSmall, hIcon) == -1) { ErrorMsg("Error creating icon ''%s'' on regular folder.",treeIconNames[i].lpName); return false; } } // Icons specified in custom ini files //printf("Trying to load %i extra custom-folder icons\n",numExtraIcons); for (i = 0; i < m_numExtraIcons; i++) { // First try standalone .ico file hIcon = LoadIconFromFile(m_ExtraFolderIcons[i]); // If no good, try an internal icon if (!hIcon) for (int j = 0; j < std::size(treeIconNames); j++) if (strcmp(m_ExtraFolderIcons[i], treeIconNames[j].lpName)==0) hIcon = LoadIcon(hInst, MAKEINTRESOURCE(treeIconNames[j].nResourceID)); // If no good, use custom.ico if (!hIcon) hIcon = LoadIcon (hInst, MAKEINTRESOURCE(IDI_FP_CUSTOM)); // Add icon to imagelist if (ImageList_AddIcon(m_hTreeSmall, hIcon) == -1) { ErrorMsg("Error creating icon ''%s'' on extra folder",m_ExtraFolderIcons[i]); return false; } } // Be sure that all the small icons were added. if (ImageList_GetImageCount(m_hTreeSmall) < numIcons) { ErrorMsg("Error with icon list--too few images. %i %i", ImageList_GetImageCount(m_hTreeSmall),numIcons); return false; } // Be sure that all the small icons were added. if (ImageList_GetImageCount (m_hTreeSmall) < ICON_MAX) { ErrorMsg("Error with icon list--too few images. %i < %i", ImageList_GetImageCount(m_hTreeSmall),(INT)ICON_MAX); return false; } // Associate the image lists with the list view control. (void)TreeView_SetImageList(GetTreeView(), m_hTreeSmall, TVSIL_NORMAL); return true; } static void TreeCtrlOnPaint(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; RECT rcClip, rcClient; HBITMAP hBackground = GetBackgroundBitmap(); MYBITMAPINFO *bmDesc = GetBackgroundInfo(); HDC hDC = BeginPaint(hWnd, &ps); GetClipBox(hDC, &rcClip); GetClientRect(hWnd, &rcClient); // Create a compatible memory DC HDC memDC = CreateCompatibleDC(hDC); // Select a compatible bitmap into the memory DC HBITMAP bitmap = CreateCompatibleBitmap(hDC, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top); HBITMAP hOldBitmap = (HBITMAP)SelectObject(memDC, bitmap); // First let the control do its default drawing. CallWindowProc(m_lpTreeWndProc, hWnd, uMsg, (WPARAM)memDC, 0); // Draw bitmap in the background // Now create a mask HDC maskDC = CreateCompatibleDC(hDC); // Create monochrome bitmap for the mask HBITMAP maskBitmap = CreateBitmap(rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, 1, 1, NULL); HBITMAP hOldMaskBitmap = (HBITMAP)SelectObject(maskDC, maskBitmap); SetBkColor(memDC, GetSysColor(COLOR_WINDOW)); // Create the mask from the memory DC BitBlt(maskDC, 0, 0, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, memDC, rcClient.left, rcClient.top, SRCCOPY); HDC tempDC = CreateCompatibleDC(hDC); HBITMAP hOldHBitmap = (HBITMAP)SelectObject(tempDC, hBackground); HDC imageDC = CreateCompatibleDC(hDC); HBITMAP bmpImage = CreateCompatibleBitmap(hDC, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top); HBITMAP hOldBmpImage = (HBITMAP)SelectObject(imageDC, bmpImage); HPALETTE hPAL = GetBackgroundPalette(); if (hPAL == NULL) hPAL = CreateHalftonePalette(hDC); if (GetDeviceCaps(hDC, RASTERCAPS) & RC_PALETTE && hPAL != NULL) { SelectPalette(hDC, hPAL, false); RealizePalette(hDC); SelectPalette(imageDC, hPAL, false); } // Get x and y offset RECT rcRoot; TreeView_GetItemRect(hWnd, TreeView_GetRoot(hWnd), &rcRoot, false); rcRoot.left = -GetScrollPos(hWnd, SB_HORZ); // Draw bitmap in tiled manner to imageDC for (int i = rcRoot.left; i < rcClient.right; i += bmDesc->bmWidth) for (int j = rcRoot.top; j < rcClient.bottom; j += bmDesc->bmHeight) BitBlt(imageDC, i, j, bmDesc->bmWidth, bmDesc->bmHeight, tempDC, 0, 0, SRCCOPY); // Set the background in memDC to black. Using SRCPAINT with black and any other // color results in the other color, thus making black the transparent color SetBkColor(memDC, RGB(0,0,0)); SetTextColor(memDC, RGB(255,255,255)); BitBlt(memDC, rcClip.left, rcClip.top, rcClip.right - rcClip.left, rcClip.bottom - rcClip.top, maskDC, rcClip.left, rcClip.top, SRCAND); // Set the foreground to black. See comment above. SetBkColor(imageDC, RGB(255,255,255)); SetTextColor(imageDC, RGB(0,0,0)); BitBlt(imageDC, rcClip.left, rcClip.top, rcClip.right - rcClip.left, rcClip.bottom - rcClip.top, maskDC, rcClip.left, rcClip.top, SRCAND); // Combine the foreground with the background BitBlt(imageDC, rcClip.left, rcClip.top, rcClip.right - rcClip.left, rcClip.bottom - rcClip.top, memDC, rcClip.left, rcClip.top, SRCPAINT); // Draw the final image to the screen BitBlt(hDC, rcClip.left, rcClip.top, rcClip.right - rcClip.left, rcClip.bottom - rcClip.top, imageDC, rcClip.left, rcClip.top, SRCCOPY); SelectObject(maskDC, hOldMaskBitmap); SelectObject(tempDC, hOldHBitmap); SelectObject(imageDC, hOldBmpImage); DeleteDC(maskDC); DeleteDC(imageDC); DeleteDC(tempDC); DeleteBitmap(bmpImage); DeleteBitmap(maskBitmap); if (GetBackgroundPalette() == NULL) { DeletePalette(hPAL); hPAL = NULL; } SelectObject(memDC, hOldBitmap); DeleteBitmap(bitmap); DeleteDC(memDC); EndPaint(hWnd, &ps); ReleaseDC(hWnd, hDC); } /* Header code - Directional Arrows */ static LRESULT CALLBACK TreeWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { if (GetBackgroundBitmap() != NULL) { switch (uMsg) { case WM_MOUSEMOVE: { if (MouseHasBeenMoved()) ShowCursor(true); break; } case WM_KEYDOWN : if (wParam == VK_F2) { if (m_lpCurrentFolder->m_dwFlags & FI_CUSTOM) { TreeView_EditLabel(hWnd,TreeView_GetSelection(hWnd)); return true; } } break; case WM_ERASEBKGND: return true; case WM_PAINT: TreeCtrlOnPaint(hWnd, uMsg, wParam, lParam); UpdateWindow(hWnd); break; } } /* message not handled */ return CallWindowProc(m_lpTreeWndProc, hWnd, uMsg, wParam, lParam); } /* * Filter code * Added 01/09/99 - MSH */ /* find a FOLDERDATA by folderID */ LPCFOLDERDATA FindFilter(DWORD folderID) { for (int i = 0; i < m_numInbuiltFolders; i++) if (m_lpFolderData[i].m_nFolderId == folderID) return &m_lpFolderData[i]; return (LPFOLDERDATA) 0; } LPTREEFOLDER GetFolderByName(int nParentId, const char *pszFolderName) { //First Get the Parent TreeviewItem //Enumerate Children for(int i = 0; i < m_numFolders/* ||m_treeFolders[i] != NULL*/; i++) { if (0 == strcmp(m_treeFolders[i]->m_lpTitle, pszFolderName)) { int nParent = m_treeFolders[i]->m_nParent; if ((nParent >= 0) && m_treeFolders[nParent]->m_nFolderId == nParentId) return m_treeFolders[i]; } } return NULL; } static int InitExtraFolders() { struct stat stat_buffer; struct _finddata_t files; int i, count = 0; char * ext; char buf[MAX_TITLE_LENGTH]{}; char curdir[MAX_PATH]{}; const std::string t = dir_get_value(24); const std::string emu_path = GetEmuPath(); const char *dir = t.c_str(); //printf("zeroing %d bytes\n",int(MAX_EXTRA_FOLDERS * MAX_EXTRA_SUBFOLDERS * sizeof(LPEXFOLDERDATA))); //printf("Not zeroing %d bytes\n",int(MAX_EXTRA_FOLDERS * MAX_EXTRA_SUBFOLDERS * m_folderBytes)); memset(m_ExtraFolderData, 0, (MAX_EXTRA_FOLDERS * MAX_EXTRA_SUBFOLDERS)* sizeof(LPEXFOLDERDATA)); // folders path not set if (!dir) return 0; // specified folder not exist if (stat(dir, &stat_buffer) != 0) return 0; // remember where we are - should be emu root _getcwd(curdir, MAX_PATH); // change to folders dir chdir(dir); for (i = 0; i < MAX_EXTRA_FOLDERS; i++) m_ExtraFolderIcons[i] = NULL; m_numExtraIcons = 0; intptr_t hLong = 0L; // process all the ini files in the current folder if ( (hLong = _findfirst("*.ini", &files)) == -1L ) { } else { do { if ((files.attrib & _A_SUBDIR) == 0) { FILE *fp; fp = fopen(files.name, "r"); if (fp != NULL) { int icon[2] = { 0, 0 }; char *p, *name; while (fgets(buf, MAX_TITLE_LENGTH, fp)) { if (buf[0] == '[') { p = strchr(buf, ']'); if (p == NULL) continue; *p = '\0'; name = &buf[1]; if (0 == strcmp(name, "FOLDER_SETTINGS")) { while (fgets(buf, MAX_TITLE_LENGTH, fp)) { name = strtok(buf, " =\r\n"); if (name == NULL) break; if (0 == strcmp(name, "RootFolderIcon")) { name = strtok(NULL, " =\r\n"); if (name != NULL) SetExtraIcons(name, &icon[0]); } if (0 == strcmp(name, "SubFolderIcon")) { name = strtok(NULL, " =\r\n"); if (name != NULL) SetExtraIcons(name, &icon[1]); } } break; } } } fclose(fp); //strcpy(buf, files.name); snprintf(buf, std::size(buf), "%s", files.name); ext = strrchr(buf, '.'); if (ext && *(ext + 1) && (core_stricmp(ext + 1, "ini")==0)) { m_ExtraFolderData[count] =(EXFOLDERDATA*) malloc(m_folderBytes); if (m_ExtraFolderData[count]) { *ext = '\0'; memset(m_ExtraFolderData[count], 0, m_folderBytes); snprintf(m_ExtraFolderData[count]->m_szTitle, std::size(m_ExtraFolderData[count]->m_szTitle), "%s", buf); m_ExtraFolderData[count]->m_nFolderId = m_next_folder_id++; m_ExtraFolderData[count]->m_nParent = -1; m_ExtraFolderData[count]->m_dwFlags = FI_CUSTOM; m_ExtraFolderData[count]->m_nIconId = icon[0] ? -icon[0] : IDI_FP_CUSTOM; m_ExtraFolderData[count]->m_nSubIconId = icon[1] ? -icon[1] : IDI_FP_DEF; //printf("extra folder with icon %i, subicon %i\n", //m_ExtraFolderData[count]->m_nIconId, //m_ExtraFolderData[count]->m_nSubIconId); count++; } } } } } while( _findnext(hLong, &files) == 0); _findclose(hLong); } if (chdir(emu_path.c_str()) < 0) chdir(curdir); return count; } void FreeExtraFolders() { int i; for (i = 0; i < m_numExtraFolders; i++) { if (m_ExtraFolderData[i]) { free(m_ExtraFolderData[i]); m_ExtraFolderData[i] = NULL; } } for (i = 0; i < m_numExtraIcons; i++) free(m_ExtraFolderIcons[i]); m_numExtraIcons = 0; } static void SetExtraIcons(char *name, int *id) { char *p = strchr(name, '.'); if (p != NULL) *p = '\0'; m_ExtraFolderIcons[m_numExtraIcons] = (char*)malloc(strlen(name) + 1); if (m_ExtraFolderIcons[m_numExtraIcons]) { *id = ICON_MAX + m_numExtraIcons; snprintf(m_ExtraFolderIcons[m_numExtraIcons], sizeof(m_ExtraFolderIcons[m_numExtraIcons]), "%s", name); m_numExtraIcons++; } } // Called to add child folders of the top level extra folders already created BOOL TryAddExtraFolderAndChildren(int parent_index) { FILE* fp = NULL; char fname[1024]{}; char readbuf[512]{}; char *p; char *name; LPTREEFOLDER lpTemp = NULL; LPTREEFOLDER lpFolder = m_treeFolders[parent_index]; int current_id = lpFolder->m_nFolderId; int id = lpFolder->m_nFolderId - MAX_FOLDERS; /* "folder\title.ini" */ const std::string t = dir_get_value(24); snprintf(fname, std::size(fname), "%s\\%s.ini", t.c_str(), m_ExtraFolderData[id]->m_szTitle); fp = fopen(fname, "r"); if (fp == NULL) return false; while ( fgets(readbuf, 63, fp) ) { /* do we have [...] ? */ if (readbuf[0] == '[') { p = strchr(readbuf, ']'); if (!p) continue; *p = '\0'; name = &readbuf[1]; /* is it [FOLDER_SETTINGS]? */ if (strcmp(name, "FOLDER_SETTINGS") == 0) { current_id = -1; continue; } else { /* it it [ROOT_FOLDER]? */ if (0 == strcmp(name, "ROOT_FOLDER")) { current_id = lpFolder->m_nFolderId; lpTemp = lpFolder; } else { /* must be [folder name] */ current_id = m_next_folder_id++; /* create a new folder with this name, and the flags for this folder as read from the registry */ //printf("creating %s sub-folder ",name);fflush(stdout); lpTemp = NewFolder(name,current_id,parent_index, m_ExtraFolderData[id]->m_nSubIconId, GetFolderFlags(m_numFolders) | FI_CUSTOM); if (!lpTemp) continue; m_ExtraFolderData[current_id] = (EXFOLDERDATA*)malloc(m_folderBytes); if (!m_ExtraFolderData[current_id]) continue; memset(m_ExtraFolderData[current_id], 0, m_folderBytes); m_ExtraFolderData[current_id]->m_nFolderId = current_id - MAX_EXTRA_FOLDERS; m_ExtraFolderData[current_id]->m_nIconId = m_ExtraFolderData[id]->m_nSubIconId; m_ExtraFolderData[current_id]->m_nParent = m_ExtraFolderData[id]->m_nFolderId; m_ExtraFolderData[current_id]->m_nSubIconId = -1; snprintf(m_ExtraFolderData[current_id]->m_szTitle, std::size(m_ExtraFolderData[current_id]->m_szTitle), "%s", name); m_ExtraFolderData[current_id]->m_dwFlags = m_ExtraFolderData[id]->m_dwFlags; AddFolder(lpTemp); } } } else if (current_id != -1) { /* string on a line by itself -- game name */ name = strtok(readbuf, " \t\r\n"); if (name == NULL) { current_id = -1; continue; } /* IMPORTANT: This assumes that all driver names are lowercase! */ for (int i = 0; name[i]; i++) name[i] = tolower(name[i]); if (lpTemp == NULL) { ErrorMsg("Error parsing %s: missing [folder name] or [ROOT_FOLDER]", fname); current_id = lpFolder->m_nFolderId; lpTemp = lpFolder; } AddGame(lpTemp,GetGameNameIndex(name)); } } if ( fp ) fclose( fp ); return true; } void GetFolders(TREEFOLDER ***folders,int *num_folders) { *folders = m_treeFolders; *num_folders = m_numFolders; } static BOOL TryRenameCustomFolderIni(LPTREEFOLDER lpFolder, const char *old_name, const char *new_name) { char filename[MAX_PATH]{}; char new_filename[MAX_PATH]{}; LPTREEFOLDER lpParent = NULL; string ini_dir = GetIniDir(); const char* inidir = ini_dir.c_str(); if (lpFolder->m_nParent >= 0) { //it is a custom SubFolder lpParent = GetFolder( lpFolder->m_nParent ); if( lpParent ) { snprintf(filename,std::size(filename),"%s\\%s\\%s.ini",inidir,lpParent->m_lpTitle, old_name ); snprintf(new_filename,std::size(new_filename),"%s\\%s\\%s.ini",inidir,lpParent->m_lpTitle, new_name ); win_move_file_utf8(filename,new_filename); } } else { //Rename the File, if it exists snprintf(filename,std::size(filename),"%s\\%s.ini",inidir,old_name ); snprintf(new_filename,std::size(new_filename),"%s\\%s.ini",inidir, new_name ); win_move_file_utf8(filename,new_filename); //Rename the Directory, if it exists snprintf(filename,std::size(filename),"%s\\%s",inidir,old_name ); snprintf(new_filename,std::size(new_filename),"%s\\%s",inidir, new_name ); win_move_file_utf8(filename,new_filename); } return true; } BOOL TryRenameCustomFolder(LPTREEFOLDER lpFolder, const char *new_name) { char filename[MAX_PATH]{}; char new_filename[MAX_PATH]{}; if (lpFolder->m_nParent >= 0) { // a child extra folder was renamed, so do the rename and save the parent // save old title char old_title[MAX_TITLE_LENGTH]{}; snprintf(old_title, std::size(old_title), "%s", lpFolder->m_lpTitle); // set new title snprintf(lpFolder->m_lpTitle, std::size(lpFolder->m_lpTitle), "%s", new_name); // try to save if (TrySaveExtraFolder(lpFolder) == false) { // failed, so free newly allocated title and restore old snprintf(lpFolder->m_lpTitle, std::size(lpFolder->m_lpTitle), "%s", old_title); return false; } TryRenameCustomFolderIni(lpFolder, old_title, new_name); // success return true; } // a parent extra folder was renamed, so rename the file const std::string t = dir_get_value(24); snprintf(new_filename,std::size(new_filename),"%s\\%s.ini", t.c_str(), new_name); snprintf(filename,std::size(filename),"%s\\%s.ini", t.c_str(), lpFolder->m_lpTitle); BOOL retval = win_move_file_utf8(filename,new_filename); if (retval) { TryRenameCustomFolderIni(lpFolder, lpFolder->m_lpTitle, new_name); snprintf(lpFolder->m_lpTitle, std::size(lpFolder->m_lpTitle), "%s", new_name); } if (!retval) { char buf[2048]{}; snprintf(buf,std::size(buf),"Error while renaming custom file %s to %s", filename,new_filename); win_message_box_utf8(GetMainWindow(), buf, MAMEUINAME, MB_OK | MB_ICONERROR); } return retval; } void AddToCustomFolder(LPTREEFOLDER lpFolder,int driver_index) { if ((lpFolder->m_dwFlags & FI_CUSTOM) == 0) { win_message_box_utf8(GetMainWindow(),"Unable to add game to non-custom folder", MAMEUINAME,MB_OK | MB_ICONERROR); return; } if (TestBit(lpFolder->m_lpGameBits,driver_index) == 0) { AddGame(lpFolder,driver_index); if (TrySaveExtraFolder(lpFolder) == false) RemoveGame(lpFolder,driver_index); // undo on error } } void RemoveFromCustomFolder(LPTREEFOLDER lpFolder,int driver_index) { if ((lpFolder->m_dwFlags & FI_CUSTOM) == 0) { win_message_box_utf8(GetMainWindow(),"Unable to remove game from non-custom folder", MAMEUINAME,MB_OK | MB_ICONERROR); return; } if (TestBit(lpFolder->m_lpGameBits,driver_index) != 0) { RemoveGame(lpFolder,driver_index); if (TrySaveExtraFolder(lpFolder) == false) AddGame(lpFolder,driver_index); // undo on error } } BOOL TrySaveExtraFolder(LPTREEFOLDER lpFolder) { char fname[MAX_PATH]{}; FILE *fp; BOOL error = false; int i,j; LPTREEFOLDER root_folder = NULL; LPEXFOLDERDATA extra_folder = NULL; for (i=0; im_nFolderId == lpFolder->m_nFolderId) { root_folder = lpFolder; extra_folder = m_ExtraFolderData[i]; break; } if (lpFolder->m_nParent >= 0 && m_ExtraFolderData[i]->m_nFolderId == m_treeFolders[lpFolder->m_nParent]->m_nFolderId) { root_folder = m_treeFolders[lpFolder->m_nParent]; extra_folder = m_ExtraFolderData[i]; break; } } if (extra_folder == NULL || root_folder == NULL) { MessageBox(GetMainWindow(), TEXT("Error finding custom file name to save"), TEXT(MAMEUINAME), MB_OK | MB_ICONERROR); return false; } /* "folder\title.ini" */ const std::string t = dir_get_value(24); snprintf( fname, std::size(fname), "%s\\%s.ini", t.c_str(), extra_folder->m_szTitle); fp = fopen(fname, "wt"); if (fp == NULL) error = true; else { TREEFOLDER *folder_data; fprintf(fp,"[FOLDER_SETTINGS]\n"); // negative values for icons means it's custom, so save 'em if (extra_folder->m_nIconId < 0) fprintf(fp, "RootFolderIcon = %s\n", m_ExtraFolderIcons[(-extra_folder->m_nIconId) - ICON_MAX]); else fprintf(fp, "RootFolderIcon = custom\n"); if (extra_folder->m_nSubIconId < 0) fprintf(fp,"SubFolderIcon = %s\n", m_ExtraFolderIcons[(-extra_folder->m_nSubIconId) - ICON_MAX]); else fprintf(fp, "SubFolderIcon = custom\n"); /* need to loop over all our TREEFOLDERs--first the root one, then each child. Start with the root */ folder_data = root_folder; fprintf(fp,"\n[ROOT_FOLDER]\n"); for (i=0;im_lpGameBits, i)) { fprintf(fp,"%s\n",driver_list::driver(i).name); } } /* look through the custom folders for ones with our root as parent */ for (j=0;jm_nParent >= 0 && m_treeFolders[folder_data->m_nParent] == root_folder) { fprintf(fp,"\n[%s]\n",folder_data->m_lpTitle); for (i=0;im_lpGameBits, i)) { fprintf(fp,"%s\n",driver_list::driver(i).name); } } } } if (fclose(fp) != 0) error = true; } if (error) { char buf[500]{}; snprintf(buf,std::size(buf),"Error while saving custom file %s",fname); win_message_box_utf8(GetMainWindow(), buf, MAMEUINAME, MB_OK | MB_ICONERROR); } return !error; } HIMAGELIST GetTreeViewIconList() { return m_hTreeSmall; } int GetTreeViewIconIndex(int icon_id) { if (icon_id < 0) return -icon_id; for (int i = 0; i < sizeof(treeIconNames) / sizeof(treeIconNames[0]); i++) if (icon_id == treeIconNames[i].nResourceID) return i; return -1; } static bool LoadCacheFolder(int parent_index, int id) { const char* fname = NULL; LPTREEFOLDER lpFolder = m_treeFolders[parent_index]; for (int j = 0; j < m_numInbuiltFolders; j++) if (strcmp(lpFolder->m_lpTitle, m_lpFolderData[j].m_lpTitle)==0) fname = m_lpFolderData[j].short_name; if (fname == NULL) return false; string val = dir_get_value(24); char s[val.size()+1]; strcpy(s, val.c_str()); char *fdir = strtok(s, ";"); // get first dir char filename[MAX_PATH]{}; snprintf(filename, std::size(filename), "%s\\%s", fdir, fname); FILE *f = fopen(filename, "r"); if (f == NULL) return false; char readbuf[256]{}; char *name = NULL; LPTREEFOLDER lpTemp = NULL; int current_id = lpFolder->m_nFolderId; while (fgets(readbuf, 63, f)) { /* do we have [...] ? */ if (readbuf[0] == '[') { char *p = strchr(readbuf, ']'); if (p == NULL) continue; *p = '\0'; name = &readbuf[1]; /* is it [FOLDER_SETTINGS]? */ if (strcmp(name, "FOLDER_SETTINGS") == 0) { current_id = -1; continue; } else { /* is it [ROOT_FOLDER]? */ if (0 == strcmp(name, "ROOT_FOLDER")) { current_id = lpFolder->m_nFolderId; lpTemp = lpFolder; } else { current_id = m_next_folder_id++; lpTemp = NewFolder(name, current_id, parent_index, id, GetFolderFlags(m_numFolders)); AddFolder(lpTemp); } } } else if (current_id != -1) { /* string on a line by itself -- game name */ name = strtok(readbuf, " \t\r\n"); if (name == NULL) { current_id = -1; continue; } AddGame(lpTemp, GetGameNameIndex(name)); } } fclose(f); return true; } static void SaveCacheFolder(int parent_index, const char *fname) { string val = dir_get_value(24); char s[val.size()+1]; strcpy(s, val.c_str()); char *fdir = strtok(s, ";"); // get first dir // create directory if needed wchar_t *temp = ui_wstring_from_utf8(fdir); BOOL res = CreateDirectory(temp, NULL); free(temp); if (!res) { if (GetLastError() == ERROR_PATH_NOT_FOUND) { printf("SaveCacheFolder: Unable to create the directory \"%s\".\n",fdir); return; } } // create/truncate file string filename = fdir + string("\\") + fname; FILE *f = fopen(filename.c_str(), "w"); if (f == NULL) { printf("SaveCacheFolder: Unable to open file %s for writing.\n",filename.c_str()); return; } // Populate the file fprintf(f, "[FOLDER_SETTINGS]\n"); fprintf(f, "RootFolderIcon = custom\n"); fprintf(f, "SubFolderIcon = custom\n"); /* need to loop over all our TREEFOLDERs--first the root one, then each child. start with the root */ LPTREEFOLDER lpFolder = m_treeFolders[parent_index]; TREEFOLDER *folder_data = lpFolder; fprintf(f, "\n[ROOT_FOLDER]\n"); int i; for (i = 0; i < driver_list::total(); i++) { if (TestBit(folder_data->m_lpGameBits, i)) fprintf(f, "%s\n", GetGameName(i).c_str()); } /* look through the custom folders for ones with our root as parent */ for (int jj = 0; jj < m_numFolders; jj++) { folder_data = m_treeFolders[jj]; if (folder_data->m_nParent >= 0 && m_treeFolders[folder_data->m_nParent] == lpFolder) { fprintf(f, "\n[%s]\n", folder_data->m_lpTitle); for (i = 0; i < driver_list::total(); i++) { if (TestBit(folder_data->m_lpGameBits, i)) fprintf(f, "%s\n", GetGameName(i).c_str()); } } } fclose(f); printf("SaveCacheFolder: Saved file %s.\n",filename.c_str()); }