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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
// license:BSD-3-Clause
// copyright-holders:Olivier Galibert, R. Belmont, Couriersud
//============================================================
//
// osdwindow.h - SDL window handling
//
//============================================================
#ifndef MAME_OSD_MODULES_OSDWINDOW_H
#define MAME_OSD_MODULES_OSDWINDOW_H
#pragma once
#include "emucore.h"
#include "osdhelper.h"
#include "../frontend/mame/ui/menuitem.h"
#include <cassert>
#include <chrono>
#include <memory>
#include <string>
#include <vector>
// standard windows headers
#ifdef OSD_WINDOWS
#include <windows.h>
#include <windowsx.h>
#include <mmsystem.h>
#endif
//============================================================
// TYPE DEFINITIONS
//============================================================
class osd_monitor_info;
class render_module;
class render_primitive_list;
class osd_renderer;
class osd_window_config
{
public:
osd_window_config() : aspect(0.0f), width(0), height(0), depth(0), refresh(0) {}
float aspect; // decoded aspect ratio FIXME: not used on windows
int width; // decoded width
int height; // decoded height
int depth; // decoded depth - only SDL
int refresh; // decoded refresh
};
class osd_window
{
public:
virtual ~osd_window();
render_target *target() const { return m_target; }
int fullscreen() const { return m_fullscreen; }
running_machine &machine() const { return m_machine; }
const std::string &title() const { return m_title; }
bool has_renderer() const { return m_renderer != nullptr; }
osd_renderer &renderer() const { return *m_renderer; }
void renderer_reset() { m_renderer.reset(); } // public because OSD object calls it directly during teardown
int index() const { return m_index; }
int prescale() const { return m_prescale; }
float pixel_aspect() const;
bool swap_xy() const;
bool keepaspect() const;
virtual osd_dim get_size() = 0;
osd_monitor_info *monitor() const { return m_monitor.get(); }
std::shared_ptr<osd_monitor_info> monitor_from_rect(const osd_rect *proposed) const;
void create_target();
void destroy();
// Clips the pointer to the bounds of this window
virtual void capture_pointer() = 0;
// Releases the pointer from a previously captured state
virtual void release_pointer() = 0;
virtual void show_pointer() = 0;
virtual void hide_pointer() = 0;
virtual void update() = 0;
virtual void complete_destroy() = 0;
protected:
static inline constexpr int CLICK_DISTANCE = 16; // in pointer units squared
static inline constexpr int TAP_DISTANCE = 49; // taps are less repeatable than clicks
struct prev_touch
{
prev_touch() = default;
prev_touch(prev_touch const &) = default;
prev_touch(prev_touch &&) = default;
prev_touch &operator=(prev_touch const &) = default;
prev_touch &operator=(prev_touch &&) = default;
std::chrono::steady_clock::time_point when = std::chrono::steady_clock::time_point::min();
int x = 0, y = 0, cnt = 0;
};
struct pointer_dev_info
{
pointer_dev_info() = default;
pointer_dev_info(pointer_dev_info const &) = default;
pointer_dev_info(pointer_dev_info &&) = default;
pointer_dev_info &operator=(pointer_dev_info const &) = default;
pointer_dev_info &operator=(pointer_dev_info &&) = default;
unsigned clear_expired_touches(std::chrono::steady_clock::time_point const &now, std::chrono::steady_clock::duration const &time);
unsigned consume_touch(unsigned i);
prev_touch touches[8];
};
struct pointer_info
{
pointer_info(pointer_info const &) = default;
pointer_info(pointer_info &&) = default;
pointer_info &operator=(pointer_info const &) = default;
pointer_info &operator=(pointer_info &&) = default;
pointer_info(unsigned i, unsigned d);
void primary_down(
int cx,
int cy,
std::chrono::steady_clock::duration const &time,
int tolerance,
bool checkprev,
std::vector<pointer_dev_info> &devices);
void check_primary_hold_drag(
int cx,
int cy,
std::chrono::steady_clock::duration const &time,
int tolerance);
std::chrono::steady_clock::time_point pressed;
unsigned index, device;
int x, y;
unsigned buttons;
int pressedx, pressedy;
int clickcnt;
};
osd_window(
running_machine &machine,
render_module &renderprovider,
int index,
const std::shared_ptr<osd_monitor_info> &monitor,
const osd_window_config &config);
bool renderer_interactive() const;
bool renderer_sdl_needs_opengl() const;
void renderer_create();
private:
void set_starting_view(int index, const char *defview, const char *view);
private:
render_target *m_target;
public:
render_primitive_list *m_primlist;
osd_window_config m_win_config;
private:
int m_index;
protected:
bool m_fullscreen;
int m_prescale;
private:
running_machine &m_machine;
render_module &m_renderprovider;
std::shared_ptr<osd_monitor_info> m_monitor;
std::unique_ptr<osd_renderer> m_renderer;
const std::string m_title;
};
inline unsigned osd_window::pointer_dev_info::clear_expired_touches(
std::chrono::steady_clock::time_point const &now,
std::chrono::steady_clock::duration const &time)
{
// find first non-expired touch (if any)
unsigned end(0);
while ((std::size(touches) > end) && touches[end].cnt && ((touches[end].when + time) < now))
touches[end++].cnt = 0;
// shift non-expired touches back if necessary
if (end)
{
unsigned pos(0);
while ((std::size(touches) > end) && touches[end].cnt)
{
touches[pos++] = std::move(touches[end]);
touches[end++].cnt = 0;
}
return pos;
}
else
{
while ((std::size(touches) > end) && touches[end].cnt)
++end;
return end;
}
}
inline unsigned osd_window::pointer_dev_info::consume_touch(unsigned i)
{
assert(std::size(touches) > i);
touches[i].cnt = 0;
unsigned pos(i + 1);
while ((std::size(touches) > pos) && touches[pos].cnt)
{
touches[i++] = std::move(touches[pos]);
touches[pos++].cnt = 0;
}
return i;
}
inline osd_window::pointer_info::pointer_info(unsigned i, unsigned d) :
pressed(std::chrono::steady_clock::time_point::min()),
index(i),
device(d),
x(-1),
y(-1),
buttons(0),
pressedx(0),
pressedy(0),
clickcnt(0)
{
}
inline void osd_window::pointer_info::primary_down(
int cx,
int cy,
std::chrono::steady_clock::duration const &time,
int tolerance,
bool checkprev,
std::vector<pointer_dev_info> &devices)
{
auto const now(std::chrono::steady_clock::now());
auto const exp(time + pressed);
if (0 > clickcnt)
{
// previous click turned into a hold/drag
clickcnt = 1;
}
else if (clickcnt)
{
// potential multi-click action
int const dx(cx - pressedx);
int const dy(cy - pressedy);
int const distance((dx * dx) + (dy * dy));
if ((exp < now) || (tolerance < distance))
clickcnt = 1;
else
++clickcnt;
}
else
{
// first click for this pointer, but may need to check previous touches
clickcnt = 1;
if (checkprev)
{
if (devices.size() > device)
{
auto &devinfo(devices[device]);
unsigned const end(devinfo.clear_expired_touches(now, time));
for (int i = 0; end > i; ++i)
{
int const dx(cx - devinfo.touches[i].x);
int const dy(cy - devinfo.touches[i].y);
int const distance((dx * dx) + (dy * dy));
if (tolerance >= distance)
{
// close match - consume it
clickcnt = devinfo.touches[i].cnt + 1;
devinfo.consume_touch(i);
break;
}
}
}
}
}
// record the time/location where the pointer was pressed
pressed = now;
pressedx = cx;
pressedy = cy;
}
inline void osd_window::pointer_info::check_primary_hold_drag(
int cx,
int cy,
std::chrono::steady_clock::duration const &time,
int tolerance)
{
// check for conversion to a (multi-)click-and-hold/drag
if (0 < clickcnt)
{
auto const now(std::chrono::steady_clock::now());
auto const exp(time + pressed);
int const dx(cx - pressedx);
int const dy(cy - pressedy);
int const distance((dx * dx) + (dy * dy));
if ((exp < now) || (tolerance < distance))
clickcnt = -clickcnt;
}
}
template <class TWindowHandle>
class osd_window_t : public osd_window
{
public:
TWindowHandle platform_window() const { return m_platform_window; }
protected:
using osd_window::osd_window;
void set_platform_window(TWindowHandle window)
{
assert(window == nullptr || m_platform_window == nullptr);
m_platform_window = window;
}
private:
TWindowHandle m_platform_window = nullptr;
};
class osd_renderer
{
public:
/* Generic flags */
static const int FLAG_NONE = 0x0000;
static const int FLAG_HAS_VECTOR_SCREEN = 0x0001;
osd_renderer(osd_window &window) : m_sliders_dirty(false), m_window(window), m_flags(0) { }
virtual ~osd_renderer() { }
osd_window &window() const { return m_window; }
bool has_flags(const int flag) const { return ((m_flags & flag)) == flag; }
void set_flags(int aflag) { m_flags |= aflag; }
void clear_flags(int aflag) { m_flags &= ~aflag; }
void notify_changed() { set_flags(FI_CHANGED); }
/* Interface to be implemented by render code */
virtual int create() = 0;
virtual render_primitive_list *get_primitives() = 0;
virtual void add_audio_to_recording(const int16_t *buffer, int samples_this_frame) { }
virtual std::vector<ui::menu_item> get_slider_list() { return m_sliders; }
virtual int draw(const int update) = 0;
virtual int xy_to_render_target(const int x, const int y, int *xt, int *yt) { return 0; }
virtual void save() { }
virtual void record() { }
virtual void toggle_fsfx() { }
virtual bool sliders_dirty() { return m_sliders_dirty; }
protected:
virtual void build_slider_list() { }
/* Internal flags */
static const int FI_CHANGED = 0x010000;
bool m_sliders_dirty;
std::vector<ui::menu_item> m_sliders;
private:
osd_window &m_window;
int m_flags;
};
//============================================================
// CONSTANTS
//============================================================
#define MAX_VIDEO_WINDOWS (4)
//============================================================
// TYPE DEFINITIONS
//============================================================
struct osd_video_config
{
// global configuration
int windowed; // start windowed?
int prescale; // prescale factor
int numscreens; // number of screens
// hardware options
int waitvsync; // spin until vsync
int syncrefresh; // sync only to refresh rate
int switchres; // switch resolutions
// d3d, accel, opengl
int filter; // enable filtering
// d3d
int triplebuf; // triple buffer
//============================================================
// SDL - options
//============================================================
int centerh;
int centerv;
// vector options
float beamwidth; // beam width
// perftest
int perftest; // print out real video fps
// X11 options
int restrictonemonitor; // in fullscreen, confine to Xinerama monitor 0
};
//============================================================
// GLOBAL VARIABLES
//============================================================
extern osd_video_config video_config;
#endif // MAME_OSD_MODULES_OSDWINDOW_H
|