blob: 21e2922e8ae4d2ca2aeb5eabb24529f4881e7f58 [file] [log] [blame]
finnur@chromium.org1e0a8cf2011-02-14 18:24:36 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
5#ifndef BASE_EVENT_RECORDER_H_
6#define BASE_EVENT_RECORDER_H_
thakis@chromium.org01d14522010-07-27 08:08:24 +09007#pragma once
initial.commit3f4a7322008-07-27 06:49:38 +09008
amanda@chromium.org5c44f992009-01-27 01:33:29 +09009#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +090010#include <windows.h>
viettrungluu@chromium.org6bde7e82010-08-15 02:44:30 +090011#include <stdio.h>
amanda@chromium.org5c44f992009-01-27 01:33:29 +090012#endif
rvargas@google.comc3b65182011-03-29 08:48:44 +090013
14#include "base/base_api.h"
initial.commit3f4a7322008-07-27 06:49:38 +090015#include "base/basictypes.h"
16
thestig@chromium.org1c939cf2009-05-01 13:31:22 +090017class FilePath;
18
initial.commit3f4a7322008-07-27 06:49:38 +090019namespace base {
20
21// A class for recording and playing back keyboard and mouse input events.
22//
23// Note - if you record events, and the playback with the windows in
24// different sizes or positions, the playback will fail. When
25// recording and playing, you should move the relevant windows
26// to constant sizes and locations.
27// TODO(mbelshe) For now this is a singleton. I believe that this class
28// could be easily modified to:
29// support two simultaneous recorders
30// be playing back events while already recording events.
31// Why? Imagine if the product had a "record a macro" feature.
32// You might be recording globally, while recording or playing back
33// a macro. I don't think two playbacks make sense.
rvargas@google.comc3b65182011-03-29 08:48:44 +090034class BASE_API EventRecorder {
initial.commit3f4a7322008-07-27 06:49:38 +090035 public:
36 // Get the singleton EventRecorder.
37 // We can only handle one recorder/player at a time.
38 static EventRecorder* current() {
39 if (!current_)
40 current_ = new EventRecorder();
41 return current_;
42 }
43
44 // Starts recording events.
45 // Will clobber the file if it already exists.
46 // Returns true on success, or false if an error occurred.
thestig@chromium.org1c939cf2009-05-01 13:31:22 +090047 bool StartRecording(const FilePath& filename);
initial.commit3f4a7322008-07-27 06:49:38 +090048
49 // Stops recording.
50 void StopRecording();
51
52 // Is the EventRecorder currently recording.
53 bool is_recording() const { return is_recording_; }
54
55 // Plays events previously recorded.
56 // Returns true on success, or false if an error occurred.
thestig@chromium.org1c939cf2009-05-01 13:31:22 +090057 bool StartPlayback(const FilePath& filename);
initial.commit3f4a7322008-07-27 06:49:38 +090058
59 // Stops playback.
60 void StopPlayback();
61
62 // Is the EventRecorder currently playing.
63 bool is_playing() const { return is_playing_; }
64
amanda@chromium.org5c44f992009-01-27 01:33:29 +090065#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +090066 // C-style callbacks for the EventRecorder.
67 // Used for internal purposes only.
68 LRESULT RecordWndProc(int nCode, WPARAM wParam, LPARAM lParam);
69 LRESULT PlaybackWndProc(int nCode, WPARAM wParam, LPARAM lParam);
amanda@chromium.org5c44f992009-01-27 01:33:29 +090070#endif
initial.commit3f4a7322008-07-27 06:49:38 +090071
72 private:
73 // Create a new EventRecorder. Events are saved to the file filename.
74 // If the file already exists, it will be deleted before recording
75 // starts.
76 explicit EventRecorder()
77 : is_recording_(false),
78 is_playing_(false),
amanda@chromium.org5c44f992009-01-27 01:33:29 +090079#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +090080 journal_hook_(NULL),
maruel@google.comfc903372008-10-01 05:50:51 +090081 file_(NULL),
amanda@chromium.org5c44f992009-01-27 01:33:29 +090082#endif
maruel@google.comfc903372008-10-01 05:50:51 +090083 playback_first_msg_time_(0),
84 playback_start_time_(0) {
finnur@chromium.org1e0a8cf2011-02-14 18:24:36 +090085#if defined(OS_WIN)
86 memset(&playback_msg_, 0, sizeof(playback_msg_));
87#endif
initial.commit3f4a7322008-07-27 06:49:38 +090088 }
89 ~EventRecorder();
90
91 static EventRecorder* current_; // Our singleton.
92
93 bool is_recording_;
94 bool is_playing_;
amanda@chromium.org5c44f992009-01-27 01:33:29 +090095#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +090096 HHOOK journal_hook_;
97 FILE* file_;
98 EVENTMSG playback_msg_;
amanda@chromium.org5c44f992009-01-27 01:33:29 +090099#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900100 int playback_first_msg_time_;
101 int playback_start_time_;
102
tfarina@chromium.org0aad8422010-06-05 01:13:51 +0900103 DISALLOW_COPY_AND_ASSIGN(EventRecorder);
initial.commit3f4a7322008-07-27 06:49:38 +0900104};
105
106} // namespace base
107
108#endif // BASE_EVENT_RECORDER_H_