1 // license:BSD-3-Clause
2 // copyright-holders:Olivier Galibert, R. Belmont
3 //============================================================
4 //
5 //  video.cpp - Mac video handling
6 //
7 //  Mac OSD by R. Belmont
8 //
9 //============================================================
10 
11 // MAME headers
12 #include "emu.h"
13 #include "rendutil.h"
14 #include "ui/uimain.h"
15 #include "emuopts.h"
16 #include "uiinput.h"
17 
18 
19 // MAMEOS headers
20 #include "video.h"
21 #include "window.h"
22 #include "osdmac.h"
23 #include "modules/lib/osdlib.h"
24 
25 //============================================================
26 //  CONSTANTS
27 //============================================================
28 
29 
30 //============================================================
31 //  GLOBAL VARIABLES
32 //============================================================
33 
34 osd_video_config video_config;
35 
36 //============================================================
37 //  LOCAL VARIABLES
38 //============================================================
39 
40 
41 //============================================================
42 //  PROTOTYPES
43 //============================================================
44 
45 static void check_osd_inputs(running_machine &machine);
46 
47 static void get_resolution(const char *defdata, const char *data, osd_window_config *config, int report_error);
48 
49 
50 //============================================================
51 //  video_init
52 //============================================================
53 
video_init()54 bool mac_osd_interface::video_init()
55 {
56 	int index;
57 
58 	// extract data from the options
59 	extract_video_config();
60 
61 	// we need the beam width in a float, contrary to what the core does.
62 	video_config.beamwidth = options().beam_width_min();
63 
64 	// initialize the window system so we can make windows
65 	if (!window_init())
66 		return false;
67 
68 	// create the windows
69 	for (index = 0; index < video_config.numscreens; index++)
70 	{
71 		osd_window_config conf;
72 		memset(&conf, 0, sizeof(conf));
73 		get_resolution(options().resolution(), options().resolution(index), &conf, true);
74 
75 		// create window ...
76 		std::shared_ptr<mac_window_info> win = std::make_shared<mac_window_info>(machine(), index, m_monitor_module->pick_monitor(reinterpret_cast<osd_options &>(options()), index), &conf);
77 
78 		if (win->window_init())
79 			return false;
80 	}
81 
82 	return true;
83 }
84 
85 //============================================================
86 //  video_exit
87 //============================================================
88 
video_exit()89 void mac_osd_interface::video_exit()
90 {
91 	window_exit();
92 }
93 
94 //============================================================
95 //  update
96 //============================================================
97 
update(bool skip_redraw)98 void mac_osd_interface::update(bool skip_redraw)
99 {
100 	osd_common_t::update(skip_redraw);
101 
102 	// if we're not skipping this redraw, update all windows
103 	if (!skip_redraw)
104 	{
105 //      profiler_mark(PROFILER_BLIT);
106 		for (auto window : osd_common_t::s_window_list)
107 			window->update();
108 //      profiler_mark(PROFILER_END);
109 	}
110 
111 	// if we're running, disable some parts of the debugger
112 	if ((machine().debug_flags & DEBUG_FLAG_OSD_ENABLED) != 0)
113 		debugger_update();
114 }
115 
116 //============================================================
117 //  input_update
118 //============================================================
119 
input_update()120 void mac_osd_interface::input_update()
121 {
122 	// poll the joystick values here
123 	process_events_buf();
124 	poll_inputs(machine());
125 	check_osd_inputs(machine());
126 }
127 
128 //============================================================
129 //  check_osd_inputs
130 //============================================================
131 
check_osd_inputs(running_machine & machine)132 static void check_osd_inputs(running_machine &machine)
133 {
134 	// check for toggling fullscreen mode
135 	if (machine.ui_input().pressed(IPT_OSD_1))
136 	{
137 		for (auto curwin : osd_common_t::s_window_list)
138 			std::static_pointer_cast<mac_window_info>(curwin)->toggle_full_screen();
139 	}
140 
141 	auto window = osd_common_t::s_window_list.front();
142 	if (machine.ui_input().pressed(IPT_OSD_2))
143 	{
144 		//FIXME: on a per window basis
145 		video_config.fullstretch = !video_config.fullstretch;
146 		window->target()->set_scale_mode(video_config.fullstretch? SCALE_FRACTIONAL : SCALE_INTEGER);
147 		machine.ui().popup_time(1, "Uneven stretch %s", video_config.fullstretch? "enabled":"disabled");
148 	}
149 
150 	if (machine.ui_input().pressed(IPT_OSD_4))
151 	{
152 		//FIXME: on a per window basis
153 		video_config.keepaspect = !video_config.keepaspect;
154 		window->target()->set_keepaspect(video_config.keepaspect);
155 		machine.ui().popup_time(1, "Keepaspect %s", video_config.keepaspect? "enabled":"disabled");
156 	}
157 
158 	//FIXME: on a per window basis
159 	if (machine.ui_input().pressed(IPT_OSD_5))
160 	{
161 		video_config.filter = !video_config.filter;
162 		machine.ui().popup_time(1, "Filter %s", video_config.filter? "enabled":"disabled");
163 	}
164 
165 	if (machine.ui_input().pressed(IPT_OSD_6))
166 		std::static_pointer_cast<mac_window_info>(window)->modify_prescale(-1);
167 
168 	if (machine.ui_input().pressed(IPT_OSD_7))
169 		std::static_pointer_cast<mac_window_info>(window)->modify_prescale(1);
170 
171 	if (machine.ui_input().pressed(IPT_OSD_8))
172 		window->renderer().record();
173 }
174 
175 //============================================================
176 //  extract_video_config
177 //============================================================
178 
extract_video_config()179 void mac_osd_interface::extract_video_config()
180 {
181 	const char *stemp;
182 
183 	// global options: extract the data
184 	video_config.windowed      = options().window();
185 	video_config.prescale      = options().prescale();
186 	video_config.filter        = options().filter();
187 	video_config.keepaspect    = options().keep_aspect();
188 	video_config.numscreens    = options().numscreens();
189 	video_config.fullstretch   = options().uneven_stretch();
190 
191 	// if we are in debug mode, never go full screen
192 	if (machine().debug_flags & DEBUG_FLAG_OSD_ENABLED)
193 		video_config.windowed = true;
194 
195 	// default to working video please
196 	video_config.novideo = 0;
197 
198 	// d3d options: extract the data
199 	stemp = options().video();
200 	if (strcmp(stemp, "auto") == 0)
201 	{
202 		stemp = "opengl";
203 	}
204 
205 	if (strcmp(stemp, OSDOPTVAL_NONE) == 0)
206 	{
207 		video_config.mode = VIDEO_MODE_SOFT;
208 		video_config.novideo = 1;
209 
210 		if (!emulator_info::standalone() && options().seconds_to_run() == 0)
211 			osd_printf_warning("Warning: -video none doesn't make much sense without -seconds_to_run\n");
212 	}
213 
214 	else if (strcmp(stemp, MACOPTVAL_OPENGL) == 0)
215 		video_config.mode = VIDEO_MODE_OPENGL;
216 	else if (strcmp(stemp, MACOPTVAL_BGFX) == 0)
217 	{
218 		video_config.mode = VIDEO_MODE_BGFX;
219 	}
220 	else
221 	{
222 		osd_printf_warning("Invalid video value %s; reverting to OpenGL\n", stemp);
223 		video_config.mode = VIDEO_MODE_OPENGL;
224 	}
225 
226 	video_config.switchres     = options().switch_res();
227 	video_config.waitvsync     = options().wait_vsync();
228 	video_config.syncrefresh   = options().sync_refresh();
229 	if (!video_config.waitvsync && video_config.syncrefresh)
230 	{
231 		osd_printf_warning("-syncrefresh specified without -waitvsync. Reverting to -nosyncrefresh\n");
232 		video_config.syncrefresh = 0;
233 	}
234 
235 	if (video_config.prescale < 1 || video_config.prescale > 8)
236 	{
237 		osd_printf_warning("Invalid prescale option, reverting to '1'\n");
238 		video_config.prescale = 1;
239 	}
240 
241 	// default to working video please
242 	video_config.forcepow2texture = options().gl_force_pow2_texture();
243 	video_config.allowtexturerect = !(options().gl_no_texture_rect());
244 	video_config.vbo         = options().gl_vbo();
245 	video_config.pbo         = options().gl_pbo();
246 	video_config.glsl        = options().gl_glsl();
247 	if ( video_config.glsl )
248 	{
249 		int i;
250 		video_config.glsl_filter = options().glsl_filter();
251 		video_config.glsl_shader_mamebm_num=0;
252 		for(i=0; i<GLSL_SHADER_MAX; i++)
253 		{
254 			stemp = options().shader_mame(i);
255 			if (stemp && strcmp(stemp, OSDOPTVAL_NONE) != 0 && strlen(stemp)>0)
256 			{
257 				video_config.glsl_shader_mamebm[i] = (char *) malloc(strlen(stemp)+1);
258 				strcpy(video_config.glsl_shader_mamebm[i], stemp);
259 				video_config.glsl_shader_mamebm_num++;
260 			} else {
261 				video_config.glsl_shader_mamebm[i] = nullptr;
262 			}
263 		}
264 		video_config.glsl_shader_scrn_num=0;
265 		for(i=0; i<GLSL_SHADER_MAX; i++)
266 		{
267 			stemp = options().shader_screen(i);
268 			if (stemp && strcmp(stemp, OSDOPTVAL_NONE) != 0 && strlen(stemp)>0)
269 			{
270 				video_config.glsl_shader_scrn[i] = (char *) malloc(strlen(stemp)+1);
271 				strcpy(video_config.glsl_shader_scrn[i], stemp);
272 				video_config.glsl_shader_scrn_num++;
273 			} else {
274 				video_config.glsl_shader_scrn[i] = nullptr;
275 			}
276 		}
277 	} else {
278 		int i;
279 		video_config.glsl_filter = 0;
280 		video_config.glsl_shader_mamebm_num=0;
281 		for(i=0; i<GLSL_SHADER_MAX; i++)
282 		{
283 			video_config.glsl_shader_mamebm[i] = nullptr;
284 		}
285 		video_config.glsl_shader_scrn_num=0;
286 		for(i=0; i<GLSL_SHADER_MAX; i++)
287 		{
288 			video_config.glsl_shader_scrn[i] = nullptr;
289 		}
290 	}
291 
292 	// misc options: sanity check values
293 
294 	// global options: sanity check values
295 	if (video_config.numscreens < 1 || video_config.numscreens > MAX_VIDEO_WINDOWS)
296 	{
297 		osd_printf_warning("Invalid numscreens value %d; reverting to 1\n", video_config.numscreens);
298 		video_config.numscreens = 1;
299 	}
300 }
301 
302 
303 //============================================================
304 //  get_resolution
305 //============================================================
306 
get_resolution(const char * defdata,const char * data,osd_window_config * config,int report_error)307 static void get_resolution(const char *defdata, const char *data, osd_window_config *config, int report_error)
308 {
309 	config->width = config->height = config->depth = config->refresh = 0;
310 	if (strcmp(data, OSDOPTVAL_AUTO) == 0)
311 	{
312 		if (strcmp(defdata, OSDOPTVAL_AUTO) == 0)
313 			return;
314 		data = defdata;
315 	}
316 
317 	if (sscanf(data, "%dx%dx%d", &config->width, &config->height, &config->depth) < 2 && report_error)
318 		osd_printf_error("Illegal resolution value = %s\n", data);
319 
320 	const char * at_pos = strchr(data, '@');
321 	if (at_pos)
322 		if (sscanf(at_pos + 1, "%d", &config->refresh) < 1 && report_error)
323 			osd_printf_error("Illegal refresh rate in resolution value = %s\n", data);
324 }
325