summaryrefslogtreecommitdiffstatshomepage
path: root/src/osd/modules/debugger/debugqt.cpp
blob: dbc638d354af828a5258154fdbbefbd4f2e6afdd (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
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
// license:BSD-3-Clause
// copyright-holders:Andrew Gardner
//============================================================
//
//  debugqt.c - SDL/QT debug window handling
//
//  SDLMAME by Olivier Galibert and R. Belmont
//
//============================================================

#include "debug_module.h"
#include "modules/osdmodule.h"

#if (USE_QTDEBUG)

#include <vector>

#include <QtWidgets/QApplication>
#include <QtCore/QAbstractEventDispatcher>
#include <QtCore/QAbstractNativeEventFilter>

#include "emu.h"
#include "config.h"
#include "debugger.h"
#include "modules/lib/osdobj_common.h"

#include "qt/logwindow.h"
#include "qt/mainwindow.h"
#include "qt/dasmwindow.h"
#include "qt/memorywindow.h"
#include "qt/breakpointswindow.h"
#include "qt/deviceswindow.h"
#include "qt/deviceinformationwindow.h"

#include "util/xmlfile.h"


class debug_qt : public osd_module, public debug_module
#if defined(_WIN32) && !defined(SDLMAME_WIN32)
, public QAbstractNativeEventFilter
#endif
{
public:
	debug_qt() : osd_module(OSD_DEBUG_PROVIDER, "qt"), debug_module(), m_machine(nullptr)
	{
	}

	virtual ~debug_qt() { }

	virtual int init(const osd_options &options) { return 0; }
	virtual void exit() { }

	virtual void init_debugger(running_machine &machine);
	virtual void wait_for_debugger(device_t &device, bool firststop);
	virtual void debugger_update();
#if defined(_WIN32) && !defined(SDLMAME_WIN32)
	virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *) Q_DECL_OVERRIDE;
#endif
private:
	running_machine *m_machine;
};


namespace {

//============================================================
//  "Global" variables to make QT happy
//============================================================

int qtArgc = 1;
char qtArg0[] = "mame";
char *qtArgv[] = { qtArg0, nullptr };

bool oneShot = true;
MainWindow *mainQtWindow = nullptr;

//============================================================
//  XML configuration save/load
//============================================================

// Global variable used to feed the xml configuration callbacks
std::vector<std::unique_ptr<WindowQtConfig> > xmlConfigurations;


void xml_configuration_load(running_machine &machine, config_type cfg_type, config_level cfg_level, util::xml::data_node const *parentnode)
{
	// We only care about system configuration files
	if ((cfg_type != config_type::SYSTEM) || !parentnode)
		return;

	xmlConfigurations.clear();

	// Configuration load
	util::xml::data_node const *wnode = nullptr;
	for (wnode = parentnode->get_child(osd::debugger::NODE_WINDOW); wnode; wnode = wnode->get_next_sibling(osd::debugger::NODE_WINDOW))
	{
		WindowQtConfig::WindowType type = (WindowQtConfig::WindowType)wnode->get_attribute_int(osd::debugger::ATTR_WINDOW_TYPE, WindowQtConfig::WIN_TYPE_UNKNOWN);
		switch (type)
		{
			case WindowQtConfig::WIN_TYPE_MAIN:               xmlConfigurations.push_back(std::make_unique<MainWindowQtConfig>()); break;
			case WindowQtConfig::WIN_TYPE_MEMORY:             xmlConfigurations.push_back(std::make_unique<MemoryWindowQtConfig>()); break;
			case WindowQtConfig::WIN_TYPE_DASM:               xmlConfigurations.push_back(std::make_unique<DasmWindowQtConfig>()); break;
			case WindowQtConfig::WIN_TYPE_LOG:                xmlConfigurations.push_back(std::make_unique<LogWindowQtConfig>()); break;
			case WindowQtConfig::WIN_TYPE_BREAK_POINTS:       xmlConfigurations.push_back(std::make_unique<BreakpointsWindowQtConfig>()); break;
			case WindowQtConfig::WIN_TYPE_DEVICES:            xmlConfigurations.push_back(std::make_unique<DevicesWindowQtConfig>()); break;
			case WindowQtConfig::WIN_TYPE_DEVICE_INFORMATION: xmlConfigurations.push_back(std::make_unique<DeviceInformationWindowQtConfig>()); break;
			default: continue;
		}
		xmlConfigurations.back()->recoverFromXmlNode(*wnode);
	}
}


void xml_configuration_save(running_machine &machine, config_type cfg_type, util::xml::data_node *parentnode)
{
	// We only save system configuration
	if (cfg_type != config_type::SYSTEM)
		return;

	for (int i = 0; i < xmlConfigurations.size(); i++)
	{
		WindowQtConfig &config = *xmlConfigurations[i];

		// Create an xml node
		util::xml::data_node *const debugger_node = parentnode->add_child(osd::debugger::NODE_WINDOW, nullptr);

		// Insert the appropriate information
		if (debugger_node)
			config.addToXmlDataNode(*debugger_node);
	}
}


void gather_save_configurations()
{
	xmlConfigurations.clear();

	// Loop over all the open windows
	foreach (QWidget *widget, QApplication::topLevelWidgets())
	{
		if (!widget->isVisible())
			continue;

		if (!widget->isWindow() || widget->windowType() != Qt::Window)
			continue;

		// Figure out its type
		if (dynamic_cast<MainWindow *>(widget))
			xmlConfigurations.push_back(std::make_unique<MainWindowQtConfig>());
		else if (dynamic_cast<MemoryWindow *>(widget))
			xmlConfigurations.push_back(std::make_unique<MemoryWindowQtConfig>());
		else if (dynamic_cast<DasmWindow *>(widget))
			xmlConfigurations.push_back(std::make_unique<DasmWindowQtConfig>());
		else if (dynamic_cast<LogWindow *>(widget))
			xmlConfigurations.push_back(std::make_unique<LogWindowQtConfig>());
		else if (dynamic_cast<BreakpointsWindow *>(widget))
			xmlConfigurations.push_back(std::make_unique<BreakpointsWindowQtConfig>());
		else if (dynamic_cast<DevicesWindow *>(widget))
			xmlConfigurations.push_back(std::make_unique<DevicesWindowQtConfig>());
		else if (dynamic_cast<DeviceInformationWindow *>(widget))
			xmlConfigurations.push_back(std::make_unique<DeviceInformationWindowQtConfig>());

		xmlConfigurations.back()->buildFromQWidget(widget);
	}
}


//============================================================
//  Utilities
//============================================================

void load_and_clear_main_window_config(std::vector<std::unique_ptr<WindowQtConfig> > &configList)
{
	for (int i = 0; i < configList.size(); i++)
	{
		WindowQtConfig &config = *configList[i];
		if (config.m_type == WindowQtConfig::WIN_TYPE_MAIN)
		{
			config.applyToQWidget(mainQtWindow);
			configList.erase(configList.begin() + i);
			break;
		}
	}
}


void setup_additional_startup_windows(running_machine &machine, std::vector<std::unique_ptr<WindowQtConfig> > &configList)
{
	for (int i = 0; i < configList.size(); i++)
	{
		WindowQtConfig &config = *configList[i];

		WindowQt *foo = nullptr;
		switch (config.m_type)
		{
		case WindowQtConfig::WIN_TYPE_MEMORY:
			foo = new MemoryWindow(machine); break;
		case WindowQtConfig::WIN_TYPE_DASM:
			foo = new DasmWindow(machine); break;
		case WindowQtConfig::WIN_TYPE_LOG:
			foo = new LogWindow(machine); break;
		case WindowQtConfig::WIN_TYPE_BREAK_POINTS:
			foo = new BreakpointsWindow(machine); break;
		case WindowQtConfig::WIN_TYPE_DEVICES:
			foo = new DevicesWindow(machine); break;
		case WindowQtConfig::WIN_TYPE_DEVICE_INFORMATION:
			foo = new DeviceInformationWindow(machine); break;
		default:
			break;
		}
		config.applyToQWidget(foo);
		foo->show();
	}
}


void bring_main_window_to_front()
{
	foreach (QWidget *widget, QApplication::topLevelWidgets())
	{
		if (dynamic_cast<MainWindow *>(widget))
		{
			widget->activateWindow();
			widget->raise();
		}
	}
}

} // anonymous namespace


//============================================================
//  Core functionality
//============================================================

#if defined(_WIN32) && !defined(SDLMAME_WIN32)
bool winwindow_qt_filter(void *message);

bool debug_qt::nativeEventFilter(const QByteArray &eventType, void *message, long *)
{
	winwindow_qt_filter(message);
	return false;
}
#endif

void debug_qt::init_debugger(running_machine &machine)
{
	if (!qApp)
	{
		// If you're starting from scratch, create a new qApp
		new QApplication(qtArgc, qtArgv);
#if defined(_WIN32) && !defined(SDLMAME_WIN32)
		QAbstractEventDispatcher::instance()->installNativeEventFilter(this);
#endif
	}
	else
	{
		// If you've done a hard reset, clear out existing widgets & get ready for re-init
		foreach (QWidget *widget, QApplication::topLevelWidgets())
		{
			if (!widget->isWindow() || widget->windowType() != Qt::Window)
				continue;
			delete widget;
		}
		oneShot = true;
	}

	m_machine = &machine;
	// Setup the configuration XML saving and loading
	machine.configuration().config_register("debugger",
			configuration_manager::load_delegate(&xml_configuration_load, &machine),
			configuration_manager::save_delegate(&xml_configuration_save, &machine));
}


//============================================================
//  Core functionality
//============================================================

#if defined(SDLMAME_UNIX) || defined(SDLMAME_WIN32)
extern int sdl_entered_debugger;
#elif defined(_WIN32)
void winwindow_update_cursor_state(running_machine &machine);
#endif

void debug_qt::wait_for_debugger(device_t &device, bool firststop)
{
#if defined(SDLMAME_UNIX) || defined(SDLMAME_WIN32)
	sdl_entered_debugger = 1;
#endif

	// Dialog initialization
	if (oneShot)
	{
		mainQtWindow = new MainWindow(*m_machine);
		load_and_clear_main_window_config(xmlConfigurations);
		setup_additional_startup_windows(*m_machine, xmlConfigurations);
		mainQtWindow->show();
		oneShot = false;
	}

	// Ensure all top level widgets are visible & bring main window to front
	foreach (QWidget *widget, QApplication::topLevelWidgets())
	{
		if (!widget->isWindow() || widget->windowType() != Qt::Window)
			continue;
		widget->show();
	}

	if (firststop)
		bring_main_window_to_front();

	// Set the main window to display the proper cpu
	mainQtWindow->setProcessor(&device);

	// Run our own QT event loop
	osd_sleep(osd_ticks_per_second() / 1000 * 50);
	qApp->processEvents(QEventLoop::AllEvents, 1);

	// Refresh everyone if requested
	if (mainQtWindow->wantsRefresh())
	{
		QWidgetList allWidgets = qApp->allWidgets();
		for (int i = 0; i < allWidgets.length(); i++)
			allWidgets[i]->update();
		mainQtWindow->clearRefreshFlag();
	}

	// Hide all top level widgets if requested
	if (mainQtWindow->wantsHide())
	{
		foreach (QWidget *widget, QApplication::topLevelWidgets())
		{
			if (!widget->isWindow() || widget->windowType() != Qt::Window)
				continue;
			widget->hide();
		}
		mainQtWindow->clearHideFlag();
	}

	// Exit if the machine has been instructed to do so (scheduled event == exit || hard_reset)
	if (m_machine->scheduled_event_pending())
	{
		// Keep a list of windows we want to save.
		// We need to do this here because by the time xml_configuration_save gets called
		// all the QT windows are already gone.
		gather_save_configurations();
	}
#if defined(_WIN32) && !defined(SDLMAME_WIN32)
	winwindow_update_cursor_state(*m_machine); // make sure the cursor isn't hidden while in debugger
#endif
}


//============================================================
//  Available for video.*
//============================================================

void debug_qt::debugger_update()
{
	qApp->processEvents(QEventLoop::AllEvents, 1);
}

#else // USE_QTDEBUG

MODULE_NOT_SUPPORTED(debug_qt, OSD_DEBUG_PROVIDER, "qt")

#endif

MODULE_DEFINITION(DEBUG_QT, debug_qt)