File: windows.c

package info (click to toggle)
sameboy 1.0.2%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,632 kB
  • sloc: ansic: 29,954; objc: 22,249; asm: 1,424; pascal: 1,373; makefile: 1,064; xml: 111
file content (120 lines) | stat: -rw-r--r-- 3,641 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#define COBJMACROS
#include <windows.h>
#include <shlobj.h>
#include <stdio.h>
#include "open_dialog.h"

static char *wc_to_utf8_alloc(const wchar_t *wide)
{
    unsigned int cb = WideCharToMultiByte(CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
    if (cb) {
        char *buffer = (char*) malloc(cb);
        if (buffer) {
            WideCharToMultiByte(CP_UTF8, 0, wide, -1, buffer, cb, NULL, NULL);
            return buffer;
        }
    }
    return NULL;
}

char *do_open_rom_dialog(void)
{
    OPENFILENAMEW dialog;
    wchar_t filename[MAX_PATH];

    filename[0] = '\0';
    memset(&dialog, 0, sizeof(dialog));
    dialog.lStructSize = sizeof(dialog);
    dialog.lpstrFile = filename;
    dialog.nMaxFile = MAX_PATH;
    dialog.lpstrFilter = L"Game Boy ROMs\0*.gb;*.gbc;*.sgb;*.isx\0All files\0*.*\0\0";
    dialog.nFilterIndex = 1;
    dialog.lpstrFileTitle = NULL;
    dialog.nMaxFileTitle = 0;
    dialog.lpstrInitialDir = NULL;
    dialog.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;

    if (GetOpenFileNameW(&dialog)) {
        return wc_to_utf8_alloc(filename);
    }

    return NULL;
}

char *do_open_folder_dialog(void)
{
    HRESULT hr, hrCoInit;
    char *ret = NULL;
    IFileOpenDialog *dialog = NULL;
    IShellItem *result = NULL;
    wchar_t *path = NULL;

    hrCoInit = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

    hr = CoCreateInstance(&CLSID_FileOpenDialog, NULL, CLSCTX_ALL, &IID_IFileOpenDialog, (LPVOID *)&dialog);
    if (FAILED(hr)) goto end;

    hr = IFileOpenDialog_SetOptions(dialog, FOS_NOCHANGEDIR | FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST | FOS_NOREADONLYRETURN);
    if (FAILED(hr)) goto end;

    hr = IFileOpenDialog_SetTitle(dialog, L"Select Boot ROMs Folder");
    if (FAILED(hr)) goto end;

    hr = IFileOpenDialog_Show(dialog, NULL);
    if (FAILED(hr)) goto end;

    hr = IFileOpenDialog_GetResult(dialog, &result);
    if (FAILED(hr)) goto end;

    hr = IShellItem_GetDisplayName(result, SIGDN_FILESYSPATH, &path);
    if (FAILED(hr)) goto end;

    ret = wc_to_utf8_alloc(path);

end:
    if (path) CoTaskMemFree((void *)path);
    if (result) IShellItem_Release(result);
    if (dialog) IFileOpenDialog_Release(dialog);
    if (SUCCEEDED(hrCoInit)) CoUninitialize();
    return ret;
}

char *do_save_recording_dialog(unsigned frequency)
{
    OPENFILENAMEW dialog;
    wchar_t filename[MAX_PATH + 5] = L"recording.wav";
    static wchar_t filter[] = L"RIFF WAVE\0*.wav\0Apple AIFF\0*.aiff;*.aif;*.aifc\0Raw PCM (Stereo _______Hz, 16-bit LE)\0*.raw;*.pcm;\0All files\0*.*\0\0";

    memset(&dialog, 0, sizeof(dialog));
    dialog.lStructSize = sizeof(dialog);
    dialog.lpstrFile = filename;
    dialog.nMaxFile = MAX_PATH;
    dialog.lpstrFilter = filter;
    swprintf(filter + sizeof("RIFF WAVE\0*.wav\0Apple AIFF\0*.aiff;*.aif;*.aifc\0Raw PCM (Stereo ") - 1,
             sizeof("_______Hz, 16-bit LE)"),
             L"%dHz, 16-bit LE)       ",
             frequency);

    dialog.nFilterIndex = 1;
    dialog.lpstrInitialDir = NULL;
    dialog.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
    
    if (GetSaveFileNameW(&dialog)) {
        if (dialog.nFileExtension == 0) {
            switch (dialog.nFilterIndex) {
                case 1:
                    wcscat(filename, L".wav");
                    break;
                case 2:
                    wcscat(filename, L".aiff");
                    break;
                case 3:
                    wcscat(filename, L".raw");
                    break;
            }
        }
        return wc_to_utf8_alloc(filename);
    }
    
    return NULL;
}