xref: /haiku/src/servers/debug/DebugWindow.cpp (revision 77fb9ca3e653f72d1d15c9f1a50c3d4287f680e0)
1 /*
2  * Copyright 2019, Adrien Destugues, pulkomandy@pulkomandy.tk.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include "DebugWindow.h"
7 
8 #include <stdio.h>
9 
10 #include <Button.h>
11 #include <Catalog.h>
12 #include <GroupLayout.h>
13 #include <LayoutBuilder.h>
14 #include <Locale.h>
15 #include <IconUtils.h>
16 #include <Resources.h>
17 #include <RadioButton.h>
18 #include <StripeView.h>
19 #include <TextView.h>
20 
21 #undef B_TRANSLATION_CONTEXT
22 #define B_TRANSLATION_CONTEXT "DebugServer"
23 
24 
25 DebugWindow::DebugWindow(const char* appName)
26 	:
27 	BWindow(BRect(0, 0, 100, 50), "Crashed program", B_MODAL_WINDOW,
28 		B_CLOSE_ON_ESCAPE | B_NOT_RESIZABLE | B_AUTO_UPDATE_SIZE_LIMITS),
29 	fBitmap(BRect(0, 0, 31, 31), B_RGBA32),
30 	fSemaphore(create_sem(0, "DebugWindow")),
31 	fAction(kActionKillTeam)
32 {
33 	BString buffer(B_TRANSLATE(
34 		"The application:\n\n      %app\n\n"
35 		"has encountered an error which prevents it from continuing. Haiku "
36 		"will terminate the application and clean up."));
37 	buffer.ReplaceFirst("%app", appName);
38 
39 	BResources resources;
40 	resources.SetToImage(B_TRANSLATION_CONTEXT);
41 	printf("init %s\n", strerror(resources.InitCheck()));
42 	size_t size;
43 	const uint8* iconData = (const uint8*)resources.LoadResource('VICN', 2,
44 		&size);
45 	printf("icon %p\n", iconData);
46 	BIconUtils::GetVectorIcon(iconData, size, &fBitmap);
47 	BStripeView *stripeView = new BStripeView(fBitmap);
48 
49 	BTextView *message = new BTextView("_tv_");
50 	message->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
51 	rgb_color textColor = ui_color(B_PANEL_TEXT_COLOR);
52 	message->SetFontAndColor(be_plain_font, B_FONT_ALL, &textColor);
53 	message->MakeEditable(false);
54 	message->MakeSelectable(false);
55 	message->SetWordWrap(true);
56 	message->SetText(buffer);
57 	message->SetExplicitMaxSize(BSize(B_SIZE_UNSET, B_SIZE_UNSET));
58 	message->SetExplicitMinSize(BSize(310, B_SIZE_UNSET));
59 
60 	BRadioButton *terminate = new BRadioButton("terminate",
61 		B_TRANSLATE("Terminate"), new BMessage(kActionKillTeam));
62 #ifdef HANDOVER_USE_DEBUGGER
63 	BRadioButton *report = new BRadioButton("report",
64 		B_TRANSLATE("Save report"), new BMessage(kActionSaveReportTeam));
65 #endif
66 	BRadioButton *debug = new BRadioButton("debug",
67 		B_TRANSLATE("Debug"), new BMessage(kActionDebugTeam));
68 	BRadioButton *core = new BRadioButton("core",
69 		B_TRANSLATE("Write core file"), new BMessage(kActionWriteCoreFile));
70 
71 	BButton *close = new BButton("close", B_TRANSLATE("Oh no!"),
72 		new BMessage(B_QUIT_REQUESTED));
73 
74 	terminate->SetValue(B_CONTROL_ON);
75 
76 	BLayoutBuilder::Group<>(this)
77 		.AddGroup(B_HORIZONTAL)
78 			.Add(stripeView)
79 			.AddGroup(B_VERTICAL)
80 				.SetInsets(B_USE_SMALL_SPACING)
81 				.Add(message)
82 				.AddGroup(B_VERTICAL, 0)
83 					.Add(terminate)
84 					.Add(debug)
85 					.Add(report)
86 					.Add(core)
87 				.End()
88 				.AddGroup(B_HORIZONTAL)
89 					.AddGlue()
90 					.Add(close)
91 				.End()
92 			.End()
93 		.End();
94 
95 	ResizeToPreferred();
96 	CenterOnScreen();
97 }
98 
99 
100 DebugWindow::~DebugWindow()
101 {
102 	delete_sem(fSemaphore);
103 }
104 
105 
106 void
107 DebugWindow::MessageReceived(BMessage* message)
108 {
109 	switch(message->what) {
110 		case B_QUIT_REQUESTED:
111 			release_sem(fSemaphore);
112 			break;
113 
114 		case kActionKillTeam:
115 		case kActionDebugTeam:
116 		case kActionWriteCoreFile:
117 		case kActionSaveReportTeam:
118 			fAction = message->what;
119 			return;
120 	}
121 
122 	BWindow::MessageReceived(message);
123 }
124 
125 
126 int32
127 DebugWindow::Go()
128 {
129 	Show();
130 
131 	// Wait for user to close the window
132 	acquire_sem(fSemaphore);
133 	return fAction;
134 }
135 
136 
137