forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TRecorder.cxx
2241 lines (1864 loc) · 73.6 KB
/
TRecorder.cxx
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @(#)root/gui:$Id$
// Author: Katerina Opocenska 11/09/2008
/*************************************************************************
* Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
//////////////////////////////////////////////////////////////////////////
// //
// ROOT EVENT RECORDING SYSTEM //
// ================================================================== //
// //
// TRecorder class provides interface for recording and replaying //
// events in ROOT. //
// Recorded events are: //
// - Commands typed by user in commandline ('new TCanvas') //
// - GUI events (mouse movement, button clicks, ...) //
// //
// All the recorded events from one session are stored in one TFile //
// and can be replayed again anytime. //
// //
// Recording //
// ================================================================== //
// //
// 1] To start recording //
// //
// TRecorder r(const char *filename, "NEW") //
// TRecorder r(const char *filename, "RECREATE") //
// //
// or: //
// //
// TRecorder *recorder = new TRecorder; //
// recorder->Start(const char *filename, ...) //
// //
// -filename Name of ROOT file in which to save //
// recorded events. //
// //
// 2] To stop recording //
// //
// recorder->Stop() //
// //
// //
// IMPORTANT: //
// State capturing is part of recording. It means that if you want to //
// record events for some object (window), creation of this object //
// must be also recorded. //
// //
// Example: //
// -------- //
// t = new TRecorder(); // Create a new recorder //
// t->Start("logfile.root"); // ! Start recording first //
// //
// c = new TCanvas(); // ! Then, create an object //
// c->Dump(); // Work with that object //
// //
// t->Stop(); // Stop recording //
// //
// It is strongly recommended to start recording with empty ROOT //
// environment, at least with no previously created ROOT GUI. //
// This ensures that only events for well known windows are stored. //
// Events for windows, which were not created during recording, //
// cannot be replayed. //
// //
// Replaying //
// =================================================================== //
// //
// 1] To start replaying //
// //
// TRecorder r(const char *filename) //
// TRecorder r(const char *filename, "READ") //
// //
// or: //
// //
// TRecorder *recorder = new TRecorder; //
// recorder->Replay(const char *filename, //
// Bool_t showMouseCursor = kTRUE); //
// //
// -filename A name of file with recorded events //
// previously created with TRecorder::Start //
// //
// -showMouseCursor If kTRUE, mouse cursor is replayed as well. //
// In that case it is not recommended to use mouse //
// during replaying. //
// //
// In general, it is not recommended to use mouse to change positions //
// and states of ROOT windows during replaying. //
// //
// IMPORTANT: //
// The state of ROOT environment before replaying of some events //
// must be exactly the same as before recording them. //
// Therefore it is strongly recommended to start both recording //
// and replaying with empty ROOT environment. //
// //
// 2] To pause replaying //
// //
// recorder->Pause() //
// //
// Replaying is stopped until recorder->Resume() is called. //
// //
// //
// 3] To resume paused replaying //
// //
// recorder->Resume() //
// //
// Resumes previously stopped replaying. //
// //
// //
// 4] To stop replaying before its end //
// //
// recorder->Stop() //
// //
//////////////////////////////////////////////////////////////////////////
#include "TRecorder.h"
#include "TROOT.h"
#include "TFile.h"
#include "TTimer.h"
#include "TTree.h"
#include "TMutex.h"
#include "TGButton.h"
#include "TGFileDialog.h"
#include "TGLabel.h"
#include "TGWindow.h"
#include "Buttons.h"
#include "TKey.h"
#include "TPaveLabel.h"
#include "TLatex.h"
#include "TVirtualDragManager.h"
#include "TGPicture.h"
#include "KeySymbols.h"
// Names of ROOT GUI events. Used for listing event logs.
const char *kRecEventNames[] = {
"KeyPress",
"KeyRelease",
"ButtonPress",
"ButtonRelease",
"MotionNotify",
"EnterNotify",
"LeaveNotify",
"FocusIn",
"FocusOut",
"Expose",
"ConfigureNotify",
"MapNotify",
"UnmapNotify",
"DestroyNotify",
"ClientMessage",
"SelectionClear",
"SelectionRequest",
"SelectionNotify",
"ColormapNotify",
"ButtonDoubleClick",
"OtherEvent"
};
// Names of TTrees in the TFile with recorded events
const char *kCmdEventTree = "CmdEvents"; // Name of TTree with commandline events
const char *kGuiEventTree = "GuiEvents"; // Name of TTree with GUI events
const char *kWindowsTree = "WindowsTree"; // Name of TTree with window IDs
const char *kExtraEventTree = "ExtraEvents"; // Name of TTree with extra events (PaveLabels and Texts)
const char *kBranchName = "MainBranch"; // Name of the main branch in all TTress
ClassImp(TRecorder)
//_____________________________________________________________________________
//
// TGCursorWindow
//
// Window used as fake mouse cursor wile replaying events.
//_____________________________________________________________________________
class TGCursorWindow : public TGFrame {
protected:
Pixmap_t fPic, fMask; // Pixmaps used as Window shape
public:
TGCursorWindow();
virtual ~TGCursorWindow();
};
static TGCursorWindow *gCursorWin = 0;
static Int_t gDecorWidth = 0;
static Int_t gDecorHeight = 0;
//______________________________________________________________________________
TGCursorWindow::TGCursorWindow() :
TGFrame(gClient->GetDefaultRoot(), 32, 32, kTempFrame)
{
// TGCursorWindow constructor.
SetWindowAttributes_t wattr;
const TGPicture *pbg = fClient->GetPicture("recursor.png");
fPic = pbg->GetPicture();
fMask = pbg->GetMask();
gVirtualX->ShapeCombineMask(fId, 0, 0, fMask);
SetBackgroundPixmap(fPic);
wattr.fMask = kWAOverrideRedirect | kWASaveUnder;
wattr.fSaveUnder = kTRUE;
wattr.fOverrideRedirect = kTRUE;
gVirtualX->ChangeWindowAttributes(fId, &wattr);
}
//______________________________________________________________________________
TGCursorWindow::~TGCursorWindow()
{
// Destructor.
}
//______________________________________________________________________________
TRecorder::TRecorder()
{
// Creates initial INACTIVE state for the recorder
fFilename = "";
fRecorderState = new TRecorderInactive();
}
//______________________________________________________________________________
TRecorder::TRecorder(const char *filename, Option_t *option)
{
// Creates a recorder with filename to replay or to record,
// depending on option (NEW or RECREATE will start recording,
// READ will start replaying)
TString opt(option);
fFilename = "";
fRecorderState = new TRecorderInactive();
if ((opt == "NEW") || (opt == "RECREATE"))
Start(filename, option);
else
Replay(filename);
}
//______________________________________________________________________________
TRecorder::~TRecorder()
{
// Destructor.
delete fRecorderState;
}
//______________________________________________________________________________
void TRecorder::Browse(TBrowser *)
{
// Browse the recorder from a ROOT file. This allows to replay a
// session from the browser.
Replay(fFilename);
}
//______________________________________________________________________________
void TRecorder::Start(const char *filename, Option_t *option, Window_t *w,
Int_t winCount)
{
// Starts recording events
fRecorderState->Start(this, filename, option, w, winCount);
}
//______________________________________________________________________________
void TRecorder::Stop(Bool_t guiCommand)
{
// Stopps recording events
fRecorderState->Stop(this, guiCommand);
}
//______________________________________________________________________________
Bool_t TRecorder::Replay(const char *filename, Bool_t showMouseCursor,
TRecorder::EReplayModes mode)
{
// Replays events from 'filename'
return fRecorderState->Replay(this, filename, showMouseCursor, mode);
}
//______________________________________________________________________________
void TRecorder::Pause()
{
// Pauses replaying
fRecorderState->Pause(this);
}
//______________________________________________________________________________
void TRecorder::Resume()
{
// Resumes replaying
fRecorderState->Resume(this);
}
//______________________________________________________________________________
void TRecorder::ReplayStop()
{
// Cancells replaying
fRecorderState->ReplayStop(this);
}
//______________________________________________________________________________
void TRecorder::ListCmd(const char *filename)
{
// Prints out recorded commandline events
fRecorderState->ListCmd(filename);
}
//______________________________________________________________________________
void TRecorder::ListGui(const char *filename)
{
// Prints out recorded GUI events
fRecorderState->ListGui(filename);
}
//______________________________________________________________________________
void TRecorder::ChangeState(TRecorderState *newstate, Bool_t delPreviousState)
{
// Changes state from the current to the passed one (newstate)
// Deletes the old state if delPreviousState = KTRUE
if (delPreviousState)
delete fRecorderState;
fRecorderState = newstate;
}
//______________________________________________________________________________
TRecorder::ERecorderState TRecorder::GetState() const
{
// Get current state of recorder.
return fRecorderState->GetState();
}
//______________________________________________________________________________
void TRecorder::PrevCanvases(const char *filename, Option_t *option)
{
// Save previous canvases in a .root file
fRecorderState->PrevCanvases(filename,option);
}
//______________________________________________________________________________
// Represents state of TRecorder when replaying
ClassImp(TRecorderReplaying)
//______________________________________________________________________________
TRecorderReplaying::TRecorderReplaying(const char *filename)
{
// Allocates all necessary data structures used for replaying
// What is allocated here is deleted in destructor
fCanv = 0;
fCmdTree = 0;
fCmdTreeCounter = 0;
fEventReplayed = kTRUE;
fExtraTree = 0;
fExtraTreeCounter = 0;
fFilterStatusBar = kFALSE;
fGuiTree = 0;
fGuiTreeCounter = 0;
fNextEvent = 0;
fRecorder = 0;
fRegWinCounter = 0;
fShowMouseCursor = kTRUE;
fWaitingForWindow = kFALSE;
fWin = 0;
fWinTree = 0;
fWinTreeEntries = 0;
fFile = TFile::Open(filename);
fCmdEvent = new TRecCmdEvent();
fGuiEvent = new TRecGuiEvent();
fExtraEvent = new TRecExtraEvent();
fWindowList = new TList();
fTimer = new TTimer();
fMutex = new TMutex(kFALSE);
if (!gCursorWin)
gCursorWin = new TGCursorWindow();
}
//______________________________________________________________________________
TRecorderReplaying::~TRecorderReplaying()
{
// Closes all signal-slot connections
// Frees all memory allocated in contructor.
fTimer->Disconnect(fTimer, "Timeout()", this, "ReplayRealtime()");
fTimer->TurnOff();
// delete fTimer;
gClient->Disconnect(gClient, "RegisteredWindow(Window_t)", this,
"RegisterWindow(Window_t)");
if (fFile) {
fFile->Close();
delete fFile;
}
delete fWindowList;
delete fCmdEvent;
delete fGuiEvent;
delete fExtraEvent;
delete fMutex;
if (gCursorWin)
gCursorWin->DeleteWindow();
gCursorWin = 0;
}
//______________________________________________________________________________
Bool_t TRecorderReplaying::Initialize(TRecorder *r, Bool_t showMouseCursor,
TRecorder::EReplayModes)
{
// Initialization of data structures for replaying.
// Start of replaying.
//
// Return value:
// - kTRUE = everything is OK and replaying has begun
// - kFALSE = non existing or invalid log file, replaying has not started
fWin = 0;
fGuiTreeCounter = 0;
fCmdTreeCounter = 0;
fExtraTreeCounter = 0;
fRegWinCounter = 0;
fRecorder = 0;
fFilterStatusBar = kFALSE;
fWaitingForWindow = kFALSE;
fEventReplayed = 1;
fRecorder = r;
fShowMouseCursor = showMouseCursor;
if (!fFile || fFile->IsZombie() || !fFile->IsOpen())
return kFALSE;
fCmdTree = (TTree*) fFile->Get(kCmdEventTree);
fWinTree = (TTree*) fFile->Get(kWindowsTree);
fGuiTree = (TTree*) fFile->Get(kGuiEventTree);
fExtraTree = (TTree*) fFile->Get(kExtraEventTree);
if (!fCmdTree || !fWinTree || ! fGuiTree || ! fExtraTree) {
Error("TRecorderReplaying::Initialize",
"The ROOT file is not valid event logfile.");
return kFALSE;
}
try {
fCmdTree->SetBranchAddress(kBranchName, &fCmdEvent);
fWinTree->SetBranchAddress(kBranchName, &fWin);
fGuiTree->SetBranchAddress(kBranchName, &fGuiEvent);
fExtraTree->SetBranchAddress(kBranchName, &fExtraEvent);
}
catch(...) {
Error("TRecorderReplaying::Initialize",
"The ROOT file is not valid event logfile");
return kFALSE;
}
// No event to replay in given ROOT file
if (!PrepareNextEvent()) {
Info("TRecorderReplaying::Initialize",
"Log file empty. No event to replay.");
return kFALSE;
}
// Number of registered windows during recording
fWinTreeEntries = fWinTree->GetEntries();
// When a window is registered during replaying,
// TRecorderReplaying::RegisterWindow(Window_t) is called
gClient->Connect("RegisteredWindow(Window_t)", "TRecorderReplaying",
this, "RegisterWindow(Window_t)");
Info("TRecorderReplaying::Initialize", "Replaying of file %s started",
fFile->GetName());
TFile *f = TFile::Open(fFile->GetName());
if (f && !f->IsZombie()) {
TIter nextkey(f->GetListOfKeys());
TKey *key;
TObject *obj;
while ((key = (TKey*)nextkey())) {
fFilterStatusBar = kTRUE;
obj = key->ReadObj();
if (!obj->InheritsFrom("TCanvas"))
continue;
fCanv = (TCanvas*) obj;
fCanv->Draw();
}
TCanvas *canvas;
TIter nextc(gROOT->GetListOfCanvases());
while ((canvas = (TCanvas*)nextc())) {
canvas->SetWindowSize(canvas->GetWindowWidth(),
canvas->GetWindowHeight());
}
fFilterStatusBar = kFALSE;
f->Close();
}
gPad = 0;
// Starts replaying
fTimer->Connect("Timeout()","TRecorderReplaying",this,"ReplayRealtime()");
fTimer->Start(0);
return kTRUE;
}
//______________________________________________________________________________
void TRecorderReplaying::RegisterWindow(Window_t w)
{
// Creates mapping for the newly registered window w and adds this
// mapping to fWindowList
//
// Called by signal whenever a new window is registered during replaying.
//
// The new window ID is mapped to the old one with the same number in the
// list of registered windows.
// It means that 1st new window is mapped to the 1st original,
// 2nd to the 2nd, Nth new to the Nth original.
if (fFilterStatusBar) {
TGWindow *win = gClient->GetWindowById(w);
if (win) {
if (win->GetParent()->InheritsFrom("TGStatusBar")) {
fFilterStatusBar = kFALSE;
return;
}
}
}
// Get original window ID that was registered as 'fRegWinCounter'th
if (fWinTreeEntries > fRegWinCounter) {
fWinTree->GetEntry(fRegWinCounter);
}
else {
// More windows registered when replaying then when recording.
// Cannot continue
Error("TRecorderReplaying::RegisterWindow",
"More windows registered than expected");
//ReplayStop(fRecorder);
return;
}
if ((gDebug > 0) && (fWaitingForWindow)) {
std::ios::fmtflags f = std::cout.flags(); // store flags
std::cout << " Window registered: new ID: " << std::hex << w <<
" previous ID: " << fWin << std::dec << std::endl;
std::cout.flags( f ); // restore flags (reset std::hex)
}
// Lock mutex for guarding access to fWindowList
fMutex->Lock();
// Increases counter of registered windows
fRegWinCounter++;
// Creates new mapping of original window (fWin) and a new one (w)
TRecWinPair *ids = new TRecWinPair(fWin, w);
// Saves the newly created mapping
fWindowList->Add(ids);
// If we are waiting for this window to be registered
// (Replaying was stopped because of that)
if (fWaitingForWindow && fGuiEvent->fWindow == fWin) {
if (gDebug > 0) {
std::ios::fmtflags f = std::cout.flags(); // store flags
std::cout << " Window " << std::hex << fGuiEvent->fWindow <<
" registered." << std::dec << std::endl;
std::cout.flags( f ); // restore flags (reset std::hex)
}
fNextEvent = fGuiEvent;
// Sets that we do not wait for this window anymore
fWaitingForWindow = kFALSE;
// Start replaying of events again
fTimer->Start(25);
}
fMutex->UnLock();
}
//______________________________________________________________________________
Bool_t TRecorderReplaying::RemapWindowReferences()
{
// All references to the old windows (IDs) in fNextEvent are replaced by
// new ones according to the mappings in fWindowList
// Lock mutex for guarding access to fWindowList
fMutex->Lock();
TRecWinPair *ids;
TListIter it(fWindowList);
Bool_t found = kFALSE;
// Iterates through the whole list of mappings
while ((ids = (TRecWinPair*)it.Next())) {
// Window that the event belongs to
if (!found && fGuiEvent->fWindow == 0) {
fGuiEvent->fWindow = gVirtualX->GetDefaultRootWindow();
found = kTRUE;
}
else if (!found && ids->fKey == fGuiEvent->fWindow) {
fGuiEvent->fWindow = ids->fValue;
found = kTRUE;
}
for (Int_t i = 0; i < 5; ++i) {
if ((Long_t) ids->fKey == fGuiEvent->fUser[i])
fGuiEvent->fUser[i] = ids->fValue;
}
if (fGuiEvent->fMasked && ids->fKey == fGuiEvent->fMasked) {
fGuiEvent->fMasked = ids->fValue;
}
}
if (!found && fGuiEvent->fWindow == 0) {
fGuiEvent->fWindow = gVirtualX->GetDefaultRootWindow();
found = kTRUE;
}
// Mapping for the event found
if (found) {
fMutex->UnLock();
return kTRUE;
}
if (gDebug > 0) {
// save actual formatting flags
std::ios_base::fmtflags org_flags = std::cout.flags();
std::cout << "fGuiTreeCounter = " << std::dec << fGuiTreeCounter <<
" No mapping found for ID " << std::hex << fGuiEvent->fWindow << std::endl;
TRecorderInactive::DumpRootEvent(fGuiEvent,0);
// restore original formatting flags
std::cout.flags(org_flags);
}
// Stopps timer and waits for the appropriate window to be registered
fTimer->Stop();
fWaitingForWindow = kTRUE;
fMutex->UnLock();
return kFALSE;
}
//______________________________________________________________________________
Bool_t TRecorderReplaying::FilterEvent(TRecGuiEvent *e)
{
// Not all the recorded events are replayed.
// Some of them are generated automatically in ROOT
// as a consequence of other events.
//
// RETURN VALUE:
// - kTRUE = passed TRecGuiEvent *e should be filtered
// (should not be replayed)
// - kFALSE = passed TRecGuiEvent *e should not be filtered
// (should be replayed)
// We do not replay any client messages except closing of windows
if (e->fType == kClientMessage) {
if ((e->fFormat == 32) && (e->fHandle != TRecGuiEvent::kROOT_MESSAGE)
&& ((Atom_t)e->fUser[0] == TRecGuiEvent::kWM_DELETE_WINDOW))
return kFALSE;
else
return kTRUE;
}
// See TRecorderRecording::SetTypeOfConfigureNotify to get know
// which kConfigureNotify events are filtered
if (e->fType == kConfigureNotify && e->fUser[4] == TRecGuiEvent::kCNFilter) {
return kTRUE;
}
if (e->fType == kOtherEvent) {
if (e->fFormat >= kGKeyPress && e->fFormat < kOtherEvent)
return kFALSE;
return kTRUE;
}
return kFALSE;
}
//______________________________________________________________________________
Bool_t TRecorderReplaying::PrepareNextEvent()
{
// Finds the next event in log file to replay and sets it to fNextEvent
//
// Reads both from CmdTree and GuiTree and chooses that event that becomes
// earlier
// - fCmdTreeCounter determines actual position in fCmdTree
// - fGuiTreeCounter determines actual position in fCmdTree
//
// If GUI event should be replayed, we must first make sure that there is
// appropriate mapping for this event
//
// RETURN VALUE:
// kFALSE = there is no event to be replayed
// kTRUE = there is still at least one event to be replayed. Cases:
// - fNextEvent = 0 => We are waiting for the appropriate
// window to be registered
// - fNextEvent != 0 => fNextEvent can be replayed (windows are
// ready)
fCmdEvent = 0;
fGuiEvent = 0;
fExtraEvent = 0;
fNextEvent = 0;
// Reads the next unreplayed commandline event to fCmdEvent
if (fCmdTree->GetEntries() > fCmdTreeCounter)
fCmdTree->GetEntry(fCmdTreeCounter);
// Reads the next unreplayed extra event to fExtraEvent
if (fExtraTree->GetEntries() > fExtraTreeCounter)
fExtraTree->GetEntry(fExtraTreeCounter);
// Reads the next unreplayed GUI event to fGuiEvent
// Skips GUI events that should not be replayed (FilterEvent call)
while (fGuiTree->GetEntries() > fGuiTreeCounter) {
fGuiTree->GetEntry(fGuiTreeCounter);
if (!fGuiEvent || !FilterEvent(fGuiEvent))
break;
fGuiTreeCounter++;
}
// Chooses which one will be fNextEvent (the next event to be replayed)
if (fCmdEvent && fGuiEvent && fExtraEvent) {
// If there are all uf them, compares their times and chooses the
// earlier one
if ((fCmdEvent->GetTime() <= fGuiEvent->GetTime()) &&
(fCmdEvent->GetTime() <= fExtraEvent->GetTime()))
fNextEvent = fCmdEvent;
else {
if (fGuiEvent->GetTime() <= fExtraEvent->GetTime())
fNextEvent = fGuiEvent;
else
fNextEvent = fExtraEvent;
}
}
else if (fCmdEvent && fGuiEvent) {
// If there are both of them, compares their times and chooses the
// earlier one
if (fCmdEvent->GetTime() <= fGuiEvent->GetTime())
fNextEvent = fCmdEvent;
else
fNextEvent = fGuiEvent;
}
else if (fCmdEvent && fExtraEvent ) {
// If there are both of them, compares their times and chooses the
// earlier one
if (fCmdEvent->GetTime() <= fExtraEvent->GetTime())
fNextEvent = fCmdEvent;
else
fNextEvent = fExtraEvent;
}
else if (fGuiEvent && fExtraEvent) {
// If there are both of them, compares their times and chooses the
// earlier one
if (fExtraEvent->GetTime() <= fGuiEvent->GetTime())
fNextEvent = fExtraEvent;
else
fNextEvent = fGuiEvent;
}
// Nor commandline neither event to replay
else if (!fCmdEvent && !fGuiEvent && !fExtraEvent)
fNextEvent = 0;
// Only GUI event to replay
else if (fGuiEvent)
fNextEvent = fGuiEvent;
// Only commandline event to replay
else if (fCmdEvent)
fNextEvent = fCmdEvent;
else
fNextEvent = fExtraEvent;
// Nothing to replay
if (fNextEvent == 0)
return kFALSE;
// Commandline event to replay
if (fNextEvent == fCmdEvent)
fCmdTreeCounter++;
// Extra event to replay
if (fNextEvent == fExtraEvent)
fExtraTreeCounter++;
// GUI event to replay
if (fNextEvent == fGuiEvent) {
// We have the new window to send this event to
if (RemapWindowReferences())
fGuiTreeCounter++;
// We do not have it yet (waiting for registraion)
else
fNextEvent = 0;
}
return kTRUE;
}
//______________________________________________________________________________
Bool_t TRecorderReplaying::CanOverlap()
{
// ButtonPress and ButtonRelease must be sometimes replayed more times
// Example: pressing of a button opens small window and user chooses
// something from that window (color)
// Window must be opened while user is choosing
if (!fGuiEvent) {
Error("TRecorderReplaying::CanOverlap()", "fGuiEvent = 0");
return kFALSE;
}
// only GUI events overlapping is allowed
if (fNextEvent->GetType() != TRecEvent::kGuiEvent)
return kFALSE;
if (gDebug > 0) {
std::cout << "Event overlapping " <<
kRecEventNames[((TRecGuiEvent*)fNextEvent)->fType] << std::endl;
TRecorderInactive::DumpRootEvent(((TRecGuiEvent*)fNextEvent), 0);
}
// GUI event
TRecGuiEvent *e = (TRecGuiEvent*) fNextEvent;
// Overlapping allowed for ButtonPress, ButtonRelease and MotionNotify
if (e->fType == kButtonPress || e->fType == kButtonRelease ||
e->fType == kMotionNotify)
return kTRUE;
return kFALSE;
}
//______________________________________________________________________________
void TRecorderReplaying::ReplayRealtime()
{
// Replays the next event.
//
// It is called when fTimer times out.
// Every time fTimer is set again to time equal to time difference between
// current two events being replayed.
//
// It can happen that execution of an event lasts different time during the
// recording and during the replaying.
// If fTimer times out too early and the previous event has not been yet
// replayed, it is usually postponed in order
// to keep events execution in the right order.
// The excpetions are determined by TRecorderReplaying::CanOverlap()
//
UInt_t keysym;
char str[2];
if ((gROOT->GetEditorMode() == kText) ||
(gROOT->GetEditorMode() == kPaveLabel)){
gROOT->SetEditorMode();
}
// If there are automatically generated ROOT events in the queue, they
// are let to be handled first
if (gVirtualX->EventsPending()) {
gSystem->ProcessEvents();
return;
}
// Previous event has not been replayed yet and it is not allowed for
// this event to be replayed more times
if (!fEventReplayed && !CanOverlap())
return;
// Event to replay prepared
if (fNextEvent) {
// Sets that fNextEvent has not been replayed yet
fEventReplayed = 0;
// Remembers its execution time to compute time difference with
// the next event
fPreviousEventTime = fNextEvent->GetTime();
// Special execution of events causing potential deadlocks
if (fNextEvent->GetType() == TRecEvent::kGuiEvent) {
TRecGuiEvent *ev = (TRecGuiEvent *)fNextEvent;
if (ev->fType == kGKeyPress && ev->fState & kKeyControlMask) {
Event_t *e = ev->CreateEvent(ev);
gVirtualX->LookupString(e, str, sizeof(str), keysym);
// catch the ctrl-s event
if ((keysym & ~0x20) == kKey_S) {
fEventReplayed = 1;
PrepareNextEvent();
ev->ReplayEvent(fShowMouseCursor);
return;
}
}
}
// REPLAYS CURRENT EVENT
fNextEvent->ReplayEvent(fShowMouseCursor);
// Sets that fNextEvent has been replayed
fEventReplayed = 1;
}
// Prepares new event for replaying
if (!PrepareNextEvent()) {
// No more events to be replayed (replaying has finished).
// Switches recorder back to INACTIVE state
Info("TRecorderReplaying::ReplayRealtime", "Replaying finished");
fRecorder->ChangeState(new TRecorderInactive());
return;
}
else {
// We have event to replay here.
// It will be replayed with the same time difference to the previous
// one as when recording.
// After given time, timer will call this method again
if (fNextEvent)
fTimer->Start(Long_t(fNextEvent->GetTime() - fPreviousEventTime));
}
}
//______________________________________________________________________________
void TRecorderReplaying::Pause(TRecorder *r)
{
// Pauses replaying
fTimer->Stop();
r->ChangeState(new TRecorderPaused(this), kFALSE);
Info("TRecorderReplaying::Pause", "Replaying paused.");
}
//______________________________________________________________________________
void TRecorderReplaying::ReplayStop(TRecorder *r)
{
// Cancels replaying
Info("TRecorderReplaying::ReplayStop", "Replaying cancelled");
r->ChangeState(new TRecorderInactive());
}
//______________________________________________________________________________
void TRecorderReplaying::Continue()
{
// Continues previously paused replaying
if (fNextEvent)
fTimer->Start(Long_t(fNextEvent->GetTime() - fPreviousEventTime));
}
//______________________________________________________________________________
// Represents state of TRecorder after its creation
ClassImp(TRecorderInactive)
//______________________________________________________________________________
void TRecorderInactive::Start(TRecorder *r, const char *filename,
Option_t *option, Window_t *w, Int_t winCount)
{
// Switches from INACTIVE state to RECORDING and starts recording
// const char *filename = name of ROOT file where to store recorded events
// Option_t *option = option for creation of ROOT file
// Window_t *w = list of IDs of recorder windows (if GUI for
// recorder is used) [0 by default]
// Int_t winCount = number of IDs it this list [0 by default]
TRecorderRecording *rec = new TRecorderRecording(r, filename, option, w, winCount);
if (rec->StartRecording()) {
r->ChangeState(rec);
r->fFilename = gSystem->BaseName(filename);
}
else
delete rec;
}
//______________________________________________________________________________
Bool_t TRecorderInactive::Replay(TRecorder *r, const char *filename,
Bool_t showMouseCursor,