summaryrefslogtreecommitdiffstatshomepage
path: root/src/osd/modules/debugger/win/debugwininfo.cpp
blob: f449d39dbf898477bf2f4f1cbb8907d6885857b0 (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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
// license:BSD-3-Clause
// copyright-holders:Aaron Giles, Vas Crabb
//============================================================
//
//  debugwininfo.c - Win32 debug window handling
//
//============================================================

#include "emu.h"
#include "debugwininfo.h"

#include "debugviewinfo.h"

#include "debugger.h"
#include "debug/debugcon.h"
#include "debug/debugcpu.h"

#include "util/xmlfile.h"

#include "window.h"
#include "winutf8.h"
#include "winutil.h"

#include "modules/lib/osdobj_common.h"

#include <cstring>


bool debugwin_info::s_window_class_registered = false;


debugwin_info::debugwin_info(debugger_windows_interface &debugger, bool is_main_console, LPCSTR title, WNDPROC handler) :
	debugbase_info(debugger),
	m_is_main_console(is_main_console),
	m_wnd(nullptr),
	m_handler(handler),
	m_minwidth(200),
	m_maxwidth(0),
	m_minheight(200),
	m_maxheight(0),
	m_ignore_char_lparam(0)
{
	register_window_class();

	m_wnd = win_create_window_ex_utf8(DEBUG_WINDOW_STYLE_EX, "MAMEDebugWindow", title, DEBUG_WINDOW_STYLE,
			0, 0, 100, 100, std::static_pointer_cast<win_window_info>(osd_common_t::s_window_list.front())->platform_window(), create_standard_menubar(), GetModuleHandleUni(), this);
	if (m_wnd == nullptr)
		return;

	RECT work_bounds;
	SystemParametersInfo(SPI_GETWORKAREA, 0, &work_bounds, 0);
	m_maxwidth = work_bounds.right - work_bounds.left;
	m_maxheight = work_bounds.bottom - work_bounds.top;
}


debugwin_info::~debugwin_info()
{
}


void debugwin_info::destroy()
{
	for (int curview = 0; curview < MAX_VIEWS; curview++)
		m_views[curview].reset();
	DestroyWindow(m_wnd);
}

bool debugwin_info::set_default_focus()
{
	return false;
}


void debugwin_info::prev_view(debugview_info *curview)
{
	// count the number of views
	int numviews;
	for (numviews = 0; numviews < MAX_VIEWS; numviews++)
	{
		if (m_views[numviews] == nullptr)
			break;
	}

	// if we have a curview, find out its index
	int curindex = 1;
	if (curview)
	{
		for (curindex = numviews - 1; curindex > 0; curindex--)
		{
			if (m_views[curindex].get() == curview)
				break;
		}
		if (curindex < 0)
			curindex = 1;
	}

	// loop until we find someone to take focus
	while (1)
	{
		// advance to the previous index
		curindex--;
		if (curindex < -1)
			curindex = numviews - 1;

		if (curindex < 0 && set_default_focus())
		{
			// negative numbers mean the focuswnd
			break;
		}
		else if (curindex >= 0 && m_views[curindex] != nullptr && m_views[curindex]->cursor_supported())
		{
			// positive numbers mean a view
			m_views[curindex]->set_focus();
			break;
		}
	}
}


void debugwin_info::next_view(debugview_info *curview)
{
	// count the number of views
	int numviews;
	for (numviews = 0; numviews < MAX_VIEWS; numviews++)
	{
		if (m_views[numviews] == nullptr)
			break;
	}

	// if we have a curview, find out its index
	int curindex = -1;
	if (curview)
	{
		for (curindex = numviews - 1; curindex > 0; curindex--)
		{
			if (m_views[curindex].get() == curview)
				break;
		}
	}

	// loop until we find someone to take focus
	while (1)
	{
		// advance to the previous index
		curindex++;
		if (curindex >= numviews)
			curindex = -1;

		if (curindex < 0 && set_default_focus())
		{
			// negative numbers mean the focuswnd
			break;
		}
		else if (curindex >= 0 && m_views[curindex] != nullptr && m_views[curindex]->cursor_supported())
		{
			// positive numbers mean a view
			m_views[curindex]->set_focus();
			break;
		}
	}
}


bool debugwin_info::handle_key(WPARAM wparam, LPARAM lparam)
{
	/* ignore any keys that are received while the debug key is down */
	if (!waiting_for_debugger() && seq_pressed())
		return true;

	switch (wparam)
	{
	case VK_F3:
		if (GetAsyncKeyState(VK_SHIFT) & 0x8000)
			SendMessage(m_wnd, WM_COMMAND, ID_HARD_RESET, 0);
		else
			SendMessage(m_wnd, WM_COMMAND, ID_SOFT_RESET, 0);
		return true;

	case VK_F4:
		if (GetAsyncKeyState(VK_MENU) & 0x8000)
		{
			/* ajg - never gets here since 'alt' seems to be captured somewhere else - menu maybe? */
			SendMessage(m_wnd, WM_COMMAND, ID_EXIT, 0);
			return true;
		}
		break;

	case VK_F5:
		SendMessage(m_wnd, WM_COMMAND, ID_RUN, 0);
		return true;

	case VK_F6:
		SendMessage(m_wnd, WM_COMMAND, ID_NEXT_CPU, 0);
		return true;

	case VK_F7:
		SendMessage(m_wnd, WM_COMMAND, ID_RUN_IRQ, 0);
		return true;

	case VK_F8:
		SendMessage(m_wnd, WM_COMMAND, ID_RUN_VBLANK, 0);
		return true;

	case VK_F10:
		SendMessage(m_wnd, WM_COMMAND, ID_STEP_OVER, 0);
		return true;

	case VK_F11:
		if ((GetAsyncKeyState(VK_SHIFT) & 0x8000) && ((GetAsyncKeyState(VK_CONTROL) & 0x8000) == 0))
			SendMessage(m_wnd, WM_COMMAND, ID_STEP_OUT, 0);
		else if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
			SendMessage(m_wnd, WM_COMMAND, ID_REWIND_STEP, 0);
		else
			SendMessage(m_wnd, WM_COMMAND, ID_STEP, 0);
		return true;

	case VK_F12:
		SendMessage(m_wnd, WM_COMMAND, ID_RUN_AND_HIDE, 0);
		return true;

	case 'M':
		if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
		{
			SendMessage(m_wnd, WM_COMMAND, ID_NEW_MEMORY_WND, 0);
			return true;
		}
		break;

	case 'D':
		if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
		{
			SendMessage(m_wnd, WM_COMMAND, ID_NEW_DISASM_WND, 0);
			return true;
		}
		break;

	case 'L':
		if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
		{
			SendMessage(m_wnd, WM_COMMAND, ID_NEW_LOG_WND, 0);
			return true;
		}
		break;

	case 'B':
		if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
		{
			SendMessage(m_wnd, WM_COMMAND, ID_NEW_POINTS_WND, 0);
			return true;
		}
		break;
	}

	return false;
}


void debugwin_info::save_configuration(util::xml::data_node &parentnode)
{
	util::xml::data_node *const node = parentnode.add_child(osd::debugger::NODE_WINDOW, nullptr);
	if (node)
		save_configuration_to_node(*node);
}


void debugwin_info::restore_configuration_from_node(util::xml::data_node const &node)
{
	// get current size to use for defaults
	RECT bounds;
	POINT origin;
	origin.x = 0;
	origin.y = 0;
	if (!GetClientRect(window(), &bounds) && ClientToScreen(window(), &origin))
		return;

	// get saved size and adjust for window chrome
	RECT desired;
	desired.left = node.get_attribute_int(osd::debugger::ATTR_WINDOW_POSITION_X, origin.x);
	desired.top = node.get_attribute_int(osd::debugger::ATTR_WINDOW_POSITION_Y, origin.y);
	desired.right = desired.left + node.get_attribute_int(osd::debugger::ATTR_WINDOW_WIDTH, bounds.right);
	desired.bottom = desired.top + node.get_attribute_int(osd::debugger::ATTR_WINDOW_HEIGHT, bounds.bottom);
	// TODO: sanity checks...
	if (!AdjustWindowRectEx(&desired, DEBUG_WINDOW_STYLE, GetMenu(window()) ? TRUE : FALSE, DEBUG_WINDOW_STYLE_EX))
		return;

	// actually move the window
	MoveWindow(
			window(),
			desired.left,
			desired.top,
			desired.right - desired.left,
			desired.bottom - desired.top,
			TRUE);

	// restrict to one monitor and avoid toolbars
	HMONITOR const nearest_monitor = MonitorFromWindow(window(), MONITOR_DEFAULTTONEAREST);
	if (nearest_monitor)
	{
		MONITORINFO info;
		std::memset(&info, 0, sizeof(info));
		info.cbSize = sizeof(info);
		if (GetMonitorInfo(nearest_monitor, &info))
		{
			if (desired.right > info.rcWork.right)
			{
				desired.left -= desired.right - info.rcWork.right;
				desired.right = info.rcWork.right;
			}
			if (desired.bottom > info.rcWork.bottom)
			{
				desired.top -= desired.bottom - info.rcWork.bottom;
				desired.bottom = info.rcWork.bottom;
			}
			if (desired.left < info.rcWork.left)
			{
				desired.right += info.rcWork.left - desired.left;
				desired.left = info.rcWork.left;
			}
			if (desired.top < info.rcWork.top)
			{
				desired.bottom += info.rcWork.top - desired.top;
				desired.top = info.rcWork.top;
			}
			desired.bottom = std::min(info.rcWork.bottom, desired.bottom);
			desired.right = std::min(info.rcWork.right, desired.right);
			MoveWindow(
					window(),
					desired.left,
					desired.top,
					desired.right - desired.left,
					desired.bottom - desired.top,
					TRUE);
		}
	}

	// sort out contents
	recompute_children();
}


void debugwin_info::recompute_children()
{
	if (m_views[0] != nullptr)
	{
		// compute a client rect
		RECT bounds;
		bounds.top = bounds.left = 0;
		bounds.right = m_views[0]->prefwidth() + (2 * EDGE_WIDTH);
		bounds.bottom = 200;
		AdjustWindowRectEx(&bounds, DEBUG_WINDOW_STYLE, FALSE, DEBUG_WINDOW_STYLE_EX);

		// clamp the min/max size
		set_maxwidth(bounds.right - bounds.left);

		// get the parent's dimensions
		RECT parent;
		GetClientRect(window(), &parent);

		// view gets the remaining space
		InflateRect(&parent, -EDGE_WIDTH, -EDGE_WIDTH);
		m_views[0]->set_bounds(parent);
	}
}


bool debugwin_info::handle_command(WPARAM wparam, LPARAM lparam)
{
	if (HIWORD(wparam) == 0)
	{
		switch (LOWORD(wparam))
		{
		case ID_NEW_MEMORY_WND:
			debugger().create_memory_window();
			return true;

		case ID_NEW_DISASM_WND:
			debugger().create_disasm_window();
			return true;

		case ID_NEW_LOG_WND:
			debugger().create_log_window();
			return true;

		case ID_NEW_POINTS_WND:
			debugger().create_points_window();
			return true;

		case ID_RUN_AND_HIDE:
			debugger().hide_all();
			[[fallthrough]];
		case ID_RUN:
			machine().debugger().console().get_visible_cpu()->debug()->go();
			return true;

		case ID_NEXT_CPU:
			machine().debugger().console().get_visible_cpu()->debug()->go_next_device();
			return true;

		case ID_RUN_VBLANK:
			machine().debugger().console().get_visible_cpu()->debug()->go_vblank();
			return true;

		case ID_RUN_IRQ:
			machine().debugger().console().get_visible_cpu()->debug()->go_interrupt();
			return true;

		case ID_STEP:
			machine().debugger().console().get_visible_cpu()->debug()->single_step();
			return true;

		case ID_STEP_OVER:
			machine().debugger().console().get_visible_cpu()->debug()->single_step_over();
			return true;

		case ID_STEP_OUT:
			machine().debugger().console().get_visible_cpu()->debug()->single_step_out();
			return true;

		case ID_REWIND_STEP:
			machine().rewind_step();

			// clear all PC & memory tracks
			for (device_t &device : device_enumerator(machine().root_device()))
			{
				device.debug()->track_pc_data_clear();
				device.debug()->track_mem_data_clear();
			}

			// update debugger and emulator window
			machine().debug_view().update_all();
			machine().debugger().refresh_display();
			return true;

		case ID_HARD_RESET:
			machine().schedule_hard_reset();
			return true;

		case ID_SOFT_RESET:
			machine().schedule_soft_reset();
			machine().debugger().console().get_visible_cpu()->debug()->go();
			return true;

		case ID_EXIT:
			set_default_focus();
			machine().schedule_exit();
			return true;
		}
	}
	return false;
}


void debugwin_info::draw_contents(HDC dc)
{
	// fill the background with light gray
	RECT parent;
	GetClientRect(m_wnd, &parent);
	FillRect(dc, &parent, (HBRUSH)GetStockObject(LTGRAY_BRUSH));

	// draw edges around all views
	for (int curview = 0; curview < MAX_VIEWS; curview++)
	{
		if (m_views[curview] != nullptr)
		{
			RECT bounds;
			m_views[curview]->get_bounds(bounds);
			draw_border(dc, bounds);
		}
	}
}


void debugwin_info::draw_border(HDC dc, RECT &bounds)
{
	ScreenToClient(m_wnd, &((POINT *)&bounds)[0]);
	ScreenToClient(m_wnd, &((POINT *)&bounds)[1]);
	InflateRect(&bounds, EDGE_WIDTH, EDGE_WIDTH);
	DrawEdge(dc, &bounds, EDGE_SUNKEN, BF_RECT);
}


void debugwin_info::save_configuration_to_node(util::xml::data_node &node)
{
	RECT bounds;
	POINT origin;
	origin.x = 0;
	origin.y = 0;
	if (GetClientRect(window(), &bounds) && ClientToScreen(window(), &origin))
	{
		node.set_attribute_int(osd::debugger::ATTR_WINDOW_POSITION_X, origin.x);
		node.set_attribute_int(osd::debugger::ATTR_WINDOW_POSITION_Y, origin.y);
		node.set_attribute_int(osd::debugger::ATTR_WINDOW_WIDTH, bounds.right);
		node.set_attribute_int(osd::debugger::ATTR_WINDOW_HEIGHT, bounds.bottom);
	}
}


void debugwin_info::draw_border(HDC dc, HWND child)
{
	RECT bounds;
	GetWindowRect(child, &bounds);
	draw_border(dc, bounds);
}


LRESULT debugwin_info::window_proc(UINT message, WPARAM wparam, LPARAM lparam)
{
	// handle a few messages
	switch (message)
	{
	// paint: draw bezels as necessary
	case WM_PAINT:
		{
			PAINTSTRUCT pstruct;
			HDC dc = BeginPaint(m_wnd, &pstruct);
			draw_contents(dc);
			EndPaint(m_wnd, &pstruct);
			break;
		}

	// keydown: handle debugger keys
	case WM_KEYDOWN:
		if (handle_key(wparam, lparam))
			set_ignore_char_lparam(lparam);
		break;

	// char: ignore chars associated with keys we've handled
	case WM_CHAR:
		if (check_ignore_char_lparam(lparam))
		{
			if (waiting_for_debugger() || !seq_pressed())
				return DefWindowProc(m_wnd, message, wparam, lparam);
		}
		break;

	// activate: set the focus
	case WM_ACTIVATE:
		if (wparam != WA_INACTIVE)
			set_default_focus();
		break;

	// get min/max info: set the minimum window size
	case WM_GETMINMAXINFO:
		{
			auto *minmax = (MINMAXINFO *)lparam;
			minmax->ptMinTrackSize.x = m_minwidth;
			minmax->ptMinTrackSize.y = m_minheight;
			minmax->ptMaxSize.x = minmax->ptMaxTrackSize.x = m_maxwidth;
			minmax->ptMaxSize.y = minmax->ptMaxTrackSize.y = m_maxheight;
			break;
		}

	// sizing: recompute child window locations
	case WM_SIZE:
	case WM_SIZING:
		recompute_children();
		InvalidateRect(m_wnd, nullptr, FALSE);
		break;

	// mouse wheel: forward to the first view
	case WM_MOUSEWHEEL:
		{
			static int units_carryover = 0;

			UINT lines_per_click;
			if (!SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &lines_per_click, 0))
				lines_per_click = 3;

			int const units = GET_WHEEL_DELTA_WPARAM(wparam) + units_carryover;
			int const clicks = units / WHEEL_DELTA;
			units_carryover = units % WHEEL_DELTA;

			int const delta = clicks * lines_per_click;
			int viewnum = 0;
			POINT point;

			// figure out which view we are hovering over
			GetCursorPos(&point);
			ScreenToClient(m_wnd, &point);
			HWND const child = ChildWindowFromPoint(m_wnd, point);
			if (child)
			{
				for (viewnum = 0; viewnum < MAX_VIEWS; viewnum++)
				{
					if ((m_views[viewnum] != nullptr) && m_views[viewnum]->owns_window(child))
						break;
				}
				if (viewnum == MAX_VIEWS)
					break;
			}

			// send the appropriate message to this view's scrollbar
			if (m_views[viewnum] != nullptr)
				m_views[viewnum]->send_vscroll(delta);

			break;
		}

	// activate: set the focus
	case WM_INITMENU:
		update_menu();
		break;

	// command: handle a comment
	case WM_COMMAND:
		if (!handle_command(wparam, lparam))
			return DefWindowProc(m_wnd, message, wparam, lparam);
		break;

	// close: close the window if it's not the main console
	case WM_CLOSE:
		if (m_is_main_console)
		{
			debugger().hide_all();
			machine().debugger().console().get_visible_cpu()->debug()->go();
		}
		else
		{
			destroy();
		}
		break;

	// destroy: close down the window
	case WM_NCDESTROY:
		m_wnd = nullptr;
		debugger().remove_window(*this);
		break;

	// everything else: defaults
	default:
		return DefWindowProc(m_wnd, message, wparam, lparam);
	}

	return 0;
}


HMENU debugwin_info::create_standard_menubar()
{
	// create the debug menu
	HMENU const debugmenu = CreatePopupMenu();
	if (debugmenu == nullptr)
		return nullptr;
	AppendMenu(debugmenu, MF_ENABLED, ID_NEW_MEMORY_WND, TEXT("New Memory Window\tCtrl+M"));
	AppendMenu(debugmenu, MF_ENABLED, ID_NEW_DISASM_WND, TEXT("New Disassembly Window\tCtrl+D"));
	AppendMenu(debugmenu, MF_ENABLED, ID_NEW_LOG_WND, TEXT("New Error Log Window\tCtrl+L"));
	AppendMenu(debugmenu, MF_ENABLED, ID_NEW_POINTS_WND, TEXT("New (Break|Watch)points Window\tCtrl+B"));
	AppendMenu(debugmenu, MF_DISABLED | MF_SEPARATOR, 0, TEXT(""));
	AppendMenu(debugmenu, MF_ENABLED, ID_RUN, TEXT("Run\tF5"));
	AppendMenu(debugmenu, MF_ENABLED, ID_RUN_AND_HIDE, TEXT("Run and Hide Debugger\tF12"));
	AppendMenu(debugmenu, MF_ENABLED, ID_NEXT_CPU, TEXT("Run to Next CPU\tF6"));
	AppendMenu(debugmenu, MF_ENABLED, ID_RUN_IRQ, TEXT("Run until Next Interrupt on This CPU\tF7"));
	AppendMenu(debugmenu, MF_ENABLED, ID_RUN_VBLANK, TEXT("Run until Next VBLANK\tF8"));
	AppendMenu(debugmenu, MF_DISABLED | MF_SEPARATOR, 0, TEXT(""));
	AppendMenu(debugmenu, MF_ENABLED, ID_STEP, TEXT("Step Into\tF11"));
	AppendMenu(debugmenu, MF_ENABLED, ID_STEP_OVER, TEXT("Step Over\tF10"));
	AppendMenu(debugmenu, MF_ENABLED, ID_STEP_OUT, TEXT("Step Out\tShift+F11"));
	AppendMenu(debugmenu, MF_ENABLED, ID_REWIND_STEP, TEXT("Rewind Step\tCtrl+F11"));
	AppendMenu(debugmenu, MF_DISABLED | MF_SEPARATOR, 0, TEXT(""));
	AppendMenu(debugmenu, MF_ENABLED, ID_SOFT_RESET, TEXT("Soft Reset\tF3"));
	AppendMenu(debugmenu, MF_ENABLED, ID_HARD_RESET, TEXT("Hard Reset\tShift+F3"));
	AppendMenu(debugmenu, MF_ENABLED, ID_EXIT, TEXT("Exit"));

	// create the menu bar
	HMENU const menubar = CreateMenu();
	if (menubar == nullptr)
	{
		DestroyMenu(debugmenu);
		return nullptr;
	}
	AppendMenu(menubar, MF_ENABLED | MF_POPUP, (UINT_PTR)debugmenu, TEXT("Debug"));

	return menubar;
}


LRESULT CALLBACK debugwin_info::static_window_proc(HWND wnd, UINT message, WPARAM wparam, LPARAM lparam)
{
	if (message == WM_CREATE)
	{
		// set the info pointer
		CREATESTRUCT const *const createinfo = (CREATESTRUCT *)lparam;
		auto *const info = (debugwin_info *)createinfo->lpCreateParams;
		SetWindowLongPtr(wnd, GWLP_USERDATA, (LONG_PTR)createinfo->lpCreateParams);
		if (info->m_handler)
			SetWindowLongPtr(wnd, GWLP_WNDPROC, (LONG_PTR)info->m_handler);
		return 0;
	}

	auto *const info = (debugwin_info *)(uintptr_t)GetWindowLongPtr(wnd, GWLP_USERDATA);
	if (info == nullptr)
		return DefWindowProc(wnd, message, wparam, lparam);

	assert((info->m_wnd == wnd) || (info->m_wnd == nullptr));
	return info->window_proc(message, wparam, lparam);
}


void debugwin_info::register_window_class()
{
	if (!s_window_class_registered)
	{
		WNDCLASS wc = { 0 };

		// initialize the description of the window class
		wc.lpszClassName    = TEXT("MAMEDebugWindow");
		wc.hInstance        = GetModuleHandleUni();
		wc.lpfnWndProc      = &debugwin_info::static_window_proc;
		wc.hCursor          = LoadCursor(nullptr, IDC_ARROW);
		wc.hIcon            = LoadIcon(wc.hInstance, MAKEINTRESOURCE(2));
		wc.lpszMenuName     = nullptr;
		wc.hbrBackground    = nullptr;
		wc.style            = 0;
		wc.cbClsExtra       = 0;
		wc.cbWndExtra       = 0;

		UnregisterClass(wc.lpszClassName, wc.hInstance);

		// register the class; fail if we can't
		if (!RegisterClass(&wc))
			fatalerror("Unable to register debug window class\n");

		s_window_class_registered = true;
	}
}