-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcbSystemView.cpp
417 lines (334 loc) · 13.2 KB
/
cbSystemView.cpp
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
/*
* cbSystemView
* Copyright (C) 2017 bluehazzard
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; this program is ONLY licensed under
* version 3 of the License, later versions are explicitly excluded.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0>.
*/
#include <sdk.h> // Code::Blocks SDK
#include <cbdebugger_interfaces.h>
#include <macrosmanager.h>
#include <configurationpanel.h>
#include <debuggermanager.h>
#include <logmanager.h>
#include <projectloader_hooks.h>
#include "cbSystemView.h"
#include "cbSVProjectConfigPanel.h"
int ID_DEBUG_WINDOW_MENU = wxNewId();
const wxString cbSystemViewPerTargetSetting::PROJECT_TARGET_NAME = wxT("#project#");
// Register the plugin with Code::Blocks.
// We are using an anonymous namespace so we don't litter the global one.
namespace
{
PluginRegistrant<cbSystemView> reg(_T("cbSystemView"));
}
// events handling
BEGIN_EVENT_TABLE(cbSystemView, cbPlugin)
// add any events you want to handle here
EVT_MENU(ID_DEBUG_WINDOW_MENU, cbSystemView::OnWindowMenu)
END_EVENT_TABLE()
// constructor
cbSystemView::cbSystemView()
{
// Make sure our resources are available.
// In the generated boilerplate code we have no resources but when
// we add some, it will be nice that this code is in place already ;)
if (!Manager::LoadResource(_T("cbSystemView.zip")))
{
NotifyMissingFile(_T("cbSystemView.zip"));
}
}
// destructor
cbSystemView::~cbSystemView()
{
}
void cbSystemView::OnAttach()
{
// do whatever initialization you need for your plugin
// NOTE: after this function, the inherited member variable
// m_IsAttached will be TRUE...
// You should check for it in other functions, because if it
// is FALSE, it means that the application did *not* "load"
// (see: does not need) this plugin...
m_window = new cbSVWindow(Manager::Get()->GetAppWindow());
CodeBlocksDockEvent evt(cbEVT_ADD_DOCK_WINDOW);
evt.name = _T("SystemView");
evt.title = _("System View Register window");
evt.pWindow = m_window;
evt.dockSide = CodeBlocksDockEvent::dsFloating;
evt.desiredSize.Set(400, 300);
evt.floatingSize.Set(400, 300);
evt.minimumSize.Set(200, 150);
Manager::Get()->ProcessEvent(evt);
typedef cbEventFunctor<cbSystemView, CodeBlocksEvent> Functor;
Manager::Get()->RegisterEventSink(cbEVT_DEBUGGER_STARTED,
new Functor(this, &cbSystemView::OnDebuggerStarted));
Manager::Get()->RegisterEventSink(cbEVT_DEBUGGER_FINISHED,
new Functor(this, &cbSystemView::OnDebuggerFinished));
Manager::Get()->RegisterEventSink(cbEVT_DEBUGGER_CONTINUED,
new Functor(this, &cbSystemView::OnDebuggerContinued));
Manager::Get()->RegisterEventSink(cbEVT_DEBUGGER_CURSOR_CHANGED,
new Functor(this, &cbSystemView::OnDebuggerCursorChanged));
Manager::Get()->RegisterEventSink(cbEVT_DEBUGGER_UPDATED,
new Functor(this, &cbSystemView::OnDebuggerUpdated));
Manager::Get()->RegisterEventSink(cbEVT_PROJECT_ACTIVATE,
new Functor(this, &cbSystemView::OnProjectActivated));
Manager::Get()->RegisterEventSink(cbEVT_BUILDTARGET_SELECTED,
new Functor(this, &cbSystemView::OnTargetSelected));
ProjectLoaderHooks::HookFunctorBase* myhook = new ProjectLoaderHooks::HookFunctor<cbSystemView>(this, &cbSystemView::OnProjectLoadingHook);
m_HookId = ProjectLoaderHooks::RegisterHook(myhook);
DebuggerManager *debuggerManager = Manager::Get()->GetDebuggerManager();
cbDebuggerMenuHandler *menuHandler = debuggerManager->GetMenuHandler();
if (menuHandler)
{
struct Item : cbDebuggerWindowMenuItem
{
explicit Item(cbSVWindow* window) : m_window(window) {}
void OnClick(bool enable) override
{
CodeBlocksDockEvent evt(enable ? cbEVT_SHOW_DOCK_WINDOW : cbEVT_HIDE_DOCK_WINDOW);
evt.pWindow = m_window;
Manager::Get()->ProcessEvent(evt);
}
bool IsEnabled() override
{
return true;
}
bool IsChecked() override
{
return IsWindowReallyShown(m_window);
}
private:
cbSVWindow* m_window;
};
menuHandler->RegisterWindowMenu(_("System View"),
_("Displays the content of MCU's registers, similar to the watches window"),
new Item(m_window));
}
}
cbSystemViewPerTargetSetting cbSystemView::GetCurrentActiveSetting()
{
cbDebuggerPlugin *dbg = Manager::Get()->GetDebuggerManager()->GetActiveDebugger();
cbProject* project = Manager::Get()->GetProjectManager()->GetActiveProject();
wxString build_target_name = project->GetActiveBuildTarget();
ProjectBuildTarget *bt = project->GetBuildTarget(build_target_name);
auto itr_pro = m_settings.find(project);
if (itr_pro == m_settings.end())
{
Manager::Get()->GetLogManager()->LogError(_T("cbSystemView::OnDebuggerStarted(): active project has no sv setting"));
return cbSystemViewPerTargetSetting();
}
auto itr_bt = itr_pro->second.m_targetSettings.find(bt);
if (itr_bt == itr_pro->second.m_targetSettings.end())
{
// the current target has no settings, so fall back to project settings
itr_bt = itr_pro->second.m_targetSettings.find(0);
}
// Load the correct svd File
return itr_bt->second;
}
void cbSystemView::OnDebuggerStarted(CodeBlocksEvent& evt)
{
LoadSVDFile();
m_window->OnDebuggerStarted();
}
void cbSystemView::OnDebuggerFinished(CodeBlocksEvent& evt)
{
//cbMessageBox(wxT("finished"));
}
void cbSystemView::OnDebuggerContinued(CodeBlocksEvent& evt)
{
m_window->OnDebuggerContinued();
}
void cbSystemView::OnDebuggerCursorChanged(CodeBlocksEvent& evt)
{
if (!IsWindowReallyShown(m_window))
return;
cbDebuggerPlugin *dbg = Manager::Get()->GetDebuggerManager()->GetActiveDebugger();
if (dbg)
dbg->RequestUpdate(cbDebuggerPlugin::DebugWindows::MemoryRange);
}
void cbSystemView::OnDebuggerUpdated(CodeBlocksEvent& evt)
{
if (cbDebuggerPlugin::DebugWindows(evt.GetInt()) != cbDebuggerPlugin::DebugWindows::MemoryRange)
return;
m_window->UpdateWatches();
}
void cbSystemView::OnRelease(bool appShutDown)
{
// do de-initialization for your plugin
// if appShutDown is true, the plugin is unloaded because Code::Blocks is being shut down,
// which means you must not use any of the SDK Managers
// NOTE: after this function, the inherited member variable
// m_IsAttached will be FALSE...
ProjectLoaderHooks::UnregisterHook(m_HookId, true);
if (m_window) //remove the Shell Terminals Notebook from its dockable window and delete it
{
CodeBlocksDockEvent evt(cbEVT_REMOVE_DOCK_WINDOW);
evt.pWindow = m_window;
Manager::Get()->ProcessEvent(evt);
m_window->Destroy();
}
m_window = nullptr;
DebuggerManager *debuggerManager = Manager::Get()->GetDebuggerManager();
cbDebuggerMenuHandler *menuHandler = debuggerManager->GetMenuHandler();
if (menuHandler)
menuHandler->UnregisterWindowMenu(_("System View"));
}
void cbSystemView::OnWindowMenu(wxCommandEvent& event)
{
CodeBlocksDockEvent evt(event.IsChecked() ? cbEVT_SHOW_DOCK_WINDOW : cbEVT_HIDE_DOCK_WINDOW);
evt.pWindow = m_window;
Manager::Get()->ProcessEvent(evt);
}
void cbSystemView::BuildMenu(wxMenuBar* menuBar)
{
//The application is offering its menubar for your plugin,
//to add any menu items you want...
//Append any items you need in the menu...
//NOTE: Be careful in here... The application's menubar is at your disposal.
}
cbConfigurationPanel* cbSystemView::GetProjectConfigurationPanel(wxWindow* parent, cbProject* project)
{
ProjectConfigPanel* panel = new ProjectConfigPanel(parent, this, project);
return panel;
}
void cbSystemView::LoadSVDFile()
{
cbSystemViewPerTargetSetting settings = GetCurrentActiveSetting();
cbProject* project = Manager::Get()->GetProjectManager()->GetActiveProject();
if (project)
{
wxString build_target_name = project->GetActiveBuildTarget();
ProjectBuildTarget *bt = project->GetBuildTarget(build_target_name);
wxString svdFilePath = settings.m_svdFilePath;
if (svdFilePath == wxEmptyString)
{
svdFilePath = m_settings[project].m_targetSettings[0].m_svdFilePath;
}
Manager::Get()->GetMacrosManager()->ReplaceMacros(svdFilePath, bt);
Manager::Get()->GetLogManager()->Log(_T("Load SVD file: ") + svdFilePath + " for target:" + build_target_name);
m_window->SetSVDFile(svdFilePath);
}
}
void cbSystemView::OnProjectActivated(CodeBlocksEvent& evt)
{
LoadSVDFile();
}
void cbSystemView::OnTargetSelected(CodeBlocksEvent& evt)
{
// Get target selected, and load the appropriate SVD file, update the grid to match, if we have a project
if (Manager::Get()->GetProjectManager()->GetActiveProject())
LoadSVDFile();
}
void cbSystemView::OnProjectLoadingHook(cbProject* project, TiXmlElement* elem, bool loading)
{
auto itr = m_settings.find(project);
if (loading)
{
if (itr == m_settings.end())
m_settings[project] = cbSystemViewSetting();
m_settings[project].LoadFromNode(elem, project);
}
else
{
if (itr == m_settings.end())
m_settings[project] = cbSystemViewSetting();
m_settings[project].SaveToNode(elem, project);
}
}
cbSystemViewSetting cbSystemView::GetSettings(cbProject* project)
{
auto itr = m_settings.find(project);
if (itr == m_settings.end())
{
m_settings[project] = cbSystemViewSetting();
}
return m_settings[project];
}
void cbSystemView::SetSettings(cbSystemViewSetting settings, cbProject* project)
{
m_settings[project] = settings;
}
void cbSystemViewPerTargetSetting::SaveToNode(TiXmlNode* node)
{
TiXmlElement* svdFileNode = node->FirstChildElement("svdFile");
if (svdFileNode == nullptr) // We have not found the right node, so we create it
svdFileNode = node->InsertEndChild(TiXmlElement("svdFile"))->ToElement();
svdFileNode->SetAttribute("path", cbU2C(m_svdFilePath));
}
void cbSystemViewPerTargetSetting::LoadFromNode(TiXmlNode* node)
{
TiXmlElement* svdFileNode = node->FirstChildElement("svdFile");
if (svdFileNode != nullptr)
{
m_svdFilePath = cbC2U(svdFileNode->Attribute("path"));
}
}
void cbSystemViewSetting::SaveToNode(TiXmlNode* node, cbProject* project)
{
// First search for the target node with the right target name
TiXmlElement* conf = node->FirstChildElement("cbSystemView");
if (!conf)
{
conf = node->InsertEndChild(TiXmlElement("cbSystemView"))->ToElement();
}
for (auto itr = m_targetSettings.begin(); itr != m_targetSettings.end(); ++itr)
{
TiXmlElement* targetNode = conf->FirstChildElement("target");
wxString settingTargetName = itr->first == 0 ? cbSystemViewPerTargetSetting::PROJECT_TARGET_NAME : itr->first->GetTitle();
while (targetNode)
{
wxString targetName = cbC2U(targetNode->Attribute("name"));
if (targetName == settingTargetName)
break;
targetNode = targetNode->NextSiblingElement("target");
}
if (targetNode == nullptr)
{
targetNode = conf->InsertEndChild(TiXmlElement("target"))->ToElement();
targetNode->SetAttribute("name", cbU2C(settingTargetName));
}
itr->second.SaveToNode(targetNode);
}
}
void cbSystemViewSetting::LoadFromNode(TiXmlNode* node, cbProject* project)
{
TiXmlElement* conf = node->FirstChildElement("cbSystemView");
if (conf)
{
TiXmlElement* targetNode = conf->FirstChildElement("target");
while (targetNode)
{
wxString targetName = cbC2U(targetNode->Attribute("name"));
ProjectBuildTarget* bt = 0;
if (targetName == cbSystemViewPerTargetSetting::PROJECT_TARGET_NAME)
{
bt = 0;
}
else
{
bt = project->GetBuildTarget(targetName);
if (bt == nullptr)
{
targetNode = targetNode->NextSiblingElement("target");
continue;
}
}
cbSystemViewPerTargetSetting trgSet;
trgSet.LoadFromNode(targetNode);
m_targetSettings[bt] = trgSet;
targetNode = targetNode->NextSiblingElement("target");
}
}
}