git.y1.nz

SameBoy

Accurate GB/GBC emulator
download: https://git.y1.nz/archives/sameboy.tar.gz
README | Files | Log | Refs | LICENSE

SDL/open_dialog/windows.c

      1 #define COBJMACROS
      2 #include <windows.h>
      3 #include <shlobj.h>
      4 #include <stdio.h>
      5 #include "open_dialog.h"
      6 
      7 static char *wc_to_utf8_alloc(const wchar_t *wide)
      8 {
      9     unsigned int cb = WideCharToMultiByte(CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
     10     if (cb) {
     11         char *buffer = (char*) malloc(cb);
     12         if (buffer) {
     13             WideCharToMultiByte(CP_UTF8, 0, wide, -1, buffer, cb, NULL, NULL);
     14             return buffer;
     15         }
     16     }
     17     return NULL;
     18 }
     19 
     20 char *do_open_rom_dialog(void)
     21 {
     22     OPENFILENAMEW dialog;
     23     wchar_t filename[MAX_PATH];
     24 
     25     filename[0] = '\0';
     26     memset(&dialog, 0, sizeof(dialog));
     27     dialog.lStructSize = sizeof(dialog);
     28     dialog.lpstrFile = filename;
     29     dialog.nMaxFile = MAX_PATH;
     30     dialog.lpstrFilter = L"Game Boy ROMs\0*.gb;*.gbc;*.sgb;*.isx\0All files\0*.*\0\0";
     31     dialog.nFilterIndex = 1;
     32     dialog.lpstrFileTitle = NULL;
     33     dialog.nMaxFileTitle = 0;
     34     dialog.lpstrInitialDir = NULL;
     35     dialog.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
     36 
     37     if (GetOpenFileNameW(&dialog)) {
     38         return wc_to_utf8_alloc(filename);
     39     }
     40 
     41     return NULL;
     42 }
     43 
     44 char *do_open_folder_dialog(void)
     45 {
     46     HRESULT hr, hrCoInit;
     47     char *ret = NULL;
     48     IFileOpenDialog *dialog = NULL;
     49     IShellItem *result = NULL;
     50     wchar_t *path = NULL;
     51 
     52     hrCoInit = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
     53 
     54     hr = CoCreateInstance(&CLSID_FileOpenDialog, NULL, CLSCTX_ALL, &IID_IFileOpenDialog, (LPVOID *)&dialog);
     55     if (FAILED(hr)) goto end;
     56 
     57     hr = IFileOpenDialog_SetOptions(dialog, FOS_NOCHANGEDIR | FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST | FOS_NOREADONLYRETURN);
     58     if (FAILED(hr)) goto end;
     59 
     60     hr = IFileOpenDialog_SetTitle(dialog, L"Select Boot ROMs Folder");
     61     if (FAILED(hr)) goto end;
     62 
     63     hr = IFileOpenDialog_Show(dialog, NULL);
     64     if (FAILED(hr)) goto end;
     65 
     66     hr = IFileOpenDialog_GetResult(dialog, &result);
     67     if (FAILED(hr)) goto end;
     68 
     69     hr = IShellItem_GetDisplayName(result, SIGDN_FILESYSPATH, &path);
     70     if (FAILED(hr)) goto end;
     71 
     72     ret = wc_to_utf8_alloc(path);
     73 
     74 end:
     75     if (path) CoTaskMemFree((void *)path);
     76     if (result) IShellItem_Release(result);
     77     if (dialog) IFileOpenDialog_Release(dialog);
     78     if (SUCCEEDED(hrCoInit)) CoUninitialize();
     79     return ret;
     80 }
     81 
     82 char *do_save_recording_dialog(unsigned frequency)
     83 {
     84     OPENFILENAMEW dialog;
     85     wchar_t filename[MAX_PATH + 5] = L"recording.wav";
     86     static wchar_t filter[] = L"RIFF WAVE\0*.wav\0Apple AIFF\0*.aiff;*.aif;*.aifc\0Raw PCM (Stereo \u200b\u200b\u200b\u200b\u200b\u200b\u200bHz, 16-bit LE)\0*.raw;*.pcm;\0All files\0*.*\0\0";
     87 
     88     memset(&dialog, 0, sizeof(dialog));
     89     dialog.lStructSize = sizeof(dialog);
     90     dialog.lpstrFile = filename;
     91     dialog.nMaxFile = MAX_PATH;
     92     dialog.lpstrFilter = filter;
     93         
     94     wchar_t temp[11];
     95     _itow(frequency, temp, 10);
     96     memcpy(filter + sizeof("RIFF WAVE\0*.wav\0Apple AIFF\0*.aiff;*.aif;*.aifc\0Raw PCM (Stereo ") - 1, temp, min(wcslen(temp), 7) * sizeof(wchar_t));
     97 
     98     dialog.nFilterIndex = 1;
     99     dialog.lpstrInitialDir = NULL;
    100     dialog.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
    101     
    102     if (GetSaveFileNameW(&dialog)) {
    103         if (dialog.nFileExtension == 0) {
    104             switch (dialog.nFilterIndex) {
    105                 case 1:
    106                     wcscat(filename, L".wav");
    107                     break;
    108                 case 2:
    109                     wcscat(filename, L".aiff");
    110                     break;
    111                 case 3:
    112                     wcscat(filename, L".raw");
    113                     break;
    114             }
    115         }
    116         return wc_to_utf8_alloc(filename);
    117     }
    118     
    119     return NULL;
    120 }

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.