summaryrefslogtreecommitdiffstatshomepage
path: root/src/osd/osdcore.cpp
blob: 250f51a90bc0e9a448da45dd5924c737cebf401c (plain) (blame)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// license:BSD-3-Clause
// copyright-holders:Aaron Giles

#include "osdcore.h"
#include <thread>
#include <chrono>

#if defined(SDLMAME_ANDROID)
#include <SDL2/SDL.h>
#endif

#ifdef _WIN32
#include <windows.h>
#include <stdio.h>
#include <shellapi.h>
#include "strconv.h"
#endif

static const int MAXSTACK = 10;
static osd_output *m_stack[MAXSTACK];
static int m_ptr = -1;

/*-------------------------------------------------
    osd_output
-------------------------------------------------*/

void osd_output::push(osd_output *delegate)
{
	if (m_ptr < MAXSTACK)
	{
		delegate->m_chain = (m_ptr >= 0 ? m_stack[m_ptr] : nullptr);
		m_ptr++;
		m_stack[m_ptr] = delegate;
	}
}

void osd_output::pop(osd_output *delegate)
{
	int f = -1;
	for (int i=0; i<=m_ptr; i++)
		if (m_stack[i] == delegate)
		{
			f = i;
			break;
		}
	if (f >= 0)
	{
		if (f < m_ptr)
			m_stack[f+1]->m_chain = m_stack[f]->m_chain;
		m_ptr--;
		for (int i = f; i <= m_ptr; i++)
			m_stack[i] = m_stack[i+1];
	}
}


/***************************************************************************
    OUTPUT MANAGEMENT
***************************************************************************/

/*-------------------------------------------------
    osd_vprintf_error - output an error to the
    appropriate callback
-------------------------------------------------*/

void osd_vprintf_error(util::format_argument_pack<std::ostream> const &args)
{
#if defined(SDLMAME_ANDROID)
	SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "%s", util::string_format(args).c_str());
#else
	if (m_ptr >= 0) m_stack[m_ptr]->output_callback(OSD_OUTPUT_CHANNEL_ERROR, args);
#endif
}


/*-------------------------------------------------
    osd_vprintf_warning - output a warning to the
    appropriate callback
-------------------------------------------------*/

void osd_vprintf_warning(util::format_argument_pack<std::ostream> const &args)
{
#if defined(SDLMAME_ANDROID)
	SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "%s", util::string_format(args).c_str());
#else
	if (m_ptr >= 0) m_stack[m_ptr]->output_callback(OSD_OUTPUT_CHANNEL_WARNING, args);
#endif
}


/*-------------------------------------------------
    osd_vprintf_info - output info text to the
    appropriate callback
-------------------------------------------------*/

void osd_vprintf_info(util::format_argument_pack<std::ostream> const &args)
{
#if defined(SDLMAME_ANDROID)
	SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "%s", util::string_format(args).c_str());
#else
	if (m_ptr >= 0) m_stack[m_ptr]->output_callback(OSD_OUTPUT_CHANNEL_INFO, args);
#endif
}


/*-------------------------------------------------
    osd_vprintf_verbose - output verbose text to
    the appropriate callback
-------------------------------------------------*/

void osd_vprintf_verbose(util::format_argument_pack<std::ostream> const &args)
{
#if defined(SDLMAME_ANDROID)
	SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_VERBOSE, "%s", util::string_format(args).c_str());
#else
	if (m_ptr >= 0) m_stack[m_ptr]->output_callback(OSD_OUTPUT_CHANNEL_VERBOSE, args);
#endif
}


/*-------------------------------------------------
    osd_vprintf_debug - output debug text to the
    appropriate callback
-------------------------------------------------*/

void osd_vprintf_debug(util::format_argument_pack<std::ostream> const &args)
{
#if defined(SDLMAME_ANDROID)
	SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "%s", util::string_format(args).c_str());
#else
	if (m_ptr >= 0) m_stack[m_ptr]->output_callback(OSD_OUTPUT_CHANNEL_DEBUG, args);
#endif
}


//============================================================
//  osd_ticks
//============================================================

osd_ticks_t osd_ticks(void)
{
	return std::chrono::high_resolution_clock::now().time_since_epoch().count();
}


//============================================================
//  osd_ticks_per_second
//============================================================

osd_ticks_t osd_ticks_per_second(void)
{
	return std::chrono::high_resolution_clock::period::den / std::chrono::high_resolution_clock::period::num;
}

//============================================================
//  osd_sleep
//============================================================

void osd_sleep(osd_ticks_t duration)
{
#ifdef WIN32
// sleep_for appears to oversleep on Windows with gcc 8
	Sleep(duration / (osd_ticks_per_second() / 1000));
#else
	std::this_thread::sleep_for(std::chrono::high_resolution_clock::duration(duration));
#endif
}


//============================================================
//  osd_get_command_line - returns command line arguments
//  in an std::vector<std::string> in UTF-8
//
//  The real purpose of this call is to hide details necessary
//  on Windows (provided that one wants to avoid using wmain)
//============================================================

std::vector<std::string> osd_get_command_line(int argc, char *argv[])
{
	std::vector<std::string> results;
#ifdef WIN32
	{
		// Get the command line from Windows
		int count;
		LPWSTR *wide_args = CommandLineToArgvW(GetCommandLineW(), &count);

		// Convert the returned command line arguments to UTF8 std::vector<std::string>
		results.reserve(count);
		for (int i = 0; i < count; i++)
		{
			std::string arg = osd::text::from_wstring(wide_args[i]);
			results.push_back(std::move(arg));
		}

		LocalFree(wide_args);
	}
#else // !WIN32
	{
		// for non Windows platforms, we are assuming that arguments are
		// already UTF-8; we just need to convert to std::vector<std::string>
		results.reserve(argc);
		for (int i = 0; i < argc; i++)
			results.emplace_back(argv[i]);
	}
#endif // WIN32
	return results;
}