1 // Copyright 2018 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "samples/pdfium_test_event_helper.h"
6
7 #include <stdio.h>
8
9 #include <string>
10 #include <vector>
11
12 #include "public/fpdf_fwlevent.h"
13 #include "public/fpdfview.h"
14 #include "testing/fx_string_testhelpers.h"
15
16 namespace {
17
GetModifiers(std::string modifiers_string)18 uint32_t GetModifiers(std::string modifiers_string) {
19 uint32_t modifiers = 0;
20 if (modifiers_string.find("shift") != std::string::npos)
21 modifiers |= FWL_EVENTFLAG_ShiftKey;
22 if (modifiers_string.find("control") != std::string::npos)
23 modifiers |= FWL_EVENTFLAG_ControlKey;
24 if (modifiers_string.find("alt") != std::string::npos)
25 modifiers |= FWL_EVENTFLAG_AltKey;
26
27 return modifiers;
28 }
29
SendCharCodeEvent(FPDF_FORMHANDLE form,FPDF_PAGE page,const std::vector<std::string> & tokens)30 void SendCharCodeEvent(FPDF_FORMHANDLE form,
31 FPDF_PAGE page,
32 const std::vector<std::string>& tokens) {
33 if (tokens.size() != 2) {
34 fprintf(stderr, "charcode: bad args\n");
35 return;
36 }
37
38 int charcode = atoi(tokens[1].c_str());
39 FORM_OnChar(form, page, charcode, 0);
40 }
41
SendKeyCodeEvent(FPDF_FORMHANDLE form,FPDF_PAGE page,const std::vector<std::string> & tokens)42 void SendKeyCodeEvent(FPDF_FORMHANDLE form,
43 FPDF_PAGE page,
44 const std::vector<std::string>& tokens) {
45 if (tokens.size() < 2 || tokens.size() > 3) {
46 fprintf(stderr, "keycode: bad args\n");
47 return;
48 }
49
50 int keycode = atoi(tokens[1].c_str());
51 uint32_t modifiers = tokens.size() >= 3 ? GetModifiers(tokens[2]) : 0;
52 FORM_OnKeyDown(form, page, keycode, modifiers);
53 FORM_OnKeyUp(form, page, keycode, modifiers);
54 }
55
SendMouseDownEvent(FPDF_FORMHANDLE form,FPDF_PAGE page,const std::vector<std::string> & tokens)56 void SendMouseDownEvent(FPDF_FORMHANDLE form,
57 FPDF_PAGE page,
58 const std::vector<std::string>& tokens) {
59 if (tokens.size() < 4 && tokens.size() > 5) {
60 fprintf(stderr, "mousedown: bad args\n");
61 return;
62 }
63
64 int x = atoi(tokens[2].c_str());
65 int y = atoi(tokens[3].c_str());
66 uint32_t modifiers = tokens.size() >= 5 ? GetModifiers(tokens[4]) : 0;
67
68 if (tokens[1] == "left")
69 FORM_OnLButtonDown(form, page, modifiers, x, y);
70 else if (tokens[1] == "right")
71 FORM_OnRButtonDown(form, page, modifiers, x, y);
72 else
73 fprintf(stderr, "mousedown: bad button name\n");
74 }
75
SendMouseUpEvent(FPDF_FORMHANDLE form,FPDF_PAGE page,const std::vector<std::string> & tokens)76 void SendMouseUpEvent(FPDF_FORMHANDLE form,
77 FPDF_PAGE page,
78 const std::vector<std::string>& tokens) {
79 if (tokens.size() < 4 && tokens.size() > 5) {
80 fprintf(stderr, "mouseup: bad args\n");
81 return;
82 }
83
84 int x = atoi(tokens[2].c_str());
85 int y = atoi(tokens[3].c_str());
86 int modifiers = tokens.size() >= 5 ? GetModifiers(tokens[4]) : 0;
87 if (tokens[1] == "left")
88 FORM_OnLButtonUp(form, page, modifiers, x, y);
89 else if (tokens[1] == "right")
90 FORM_OnRButtonUp(form, page, modifiers, x, y);
91 else
92 fprintf(stderr, "mouseup: bad button name\n");
93 }
94
SendMouseDoubleClickEvent(FPDF_FORMHANDLE form,FPDF_PAGE page,const std::vector<std::string> & tokens)95 void SendMouseDoubleClickEvent(FPDF_FORMHANDLE form,
96 FPDF_PAGE page,
97 const std::vector<std::string>& tokens) {
98 if (tokens.size() < 4 && tokens.size() > 5) {
99 fprintf(stderr, "mousedoubleclick: bad args\n");
100 return;
101 }
102
103 int x = atoi(tokens[2].c_str());
104 int y = atoi(tokens[3].c_str());
105 int modifiers = tokens.size() >= 5 ? GetModifiers(tokens[4]) : 0;
106 if (tokens[1] != "left") {
107 fprintf(stderr, "mousedoubleclick: bad button name\n");
108 return;
109 }
110 FORM_OnLButtonDoubleClick(form, page, modifiers, x, y);
111 }
112
SendMouseMoveEvent(FPDF_FORMHANDLE form,FPDF_PAGE page,const std::vector<std::string> & tokens)113 void SendMouseMoveEvent(FPDF_FORMHANDLE form,
114 FPDF_PAGE page,
115 const std::vector<std::string>& tokens) {
116 if (tokens.size() != 3) {
117 fprintf(stderr, "mousemove: bad args\n");
118 return;
119 }
120
121 int x = atoi(tokens[1].c_str());
122 int y = atoi(tokens[2].c_str());
123 FORM_OnMouseMove(form, page, 0, x, y);
124 }
125
SendMouseWheelEvent(FPDF_FORMHANDLE form,FPDF_PAGE page,const std::vector<std::string> & tokens)126 void SendMouseWheelEvent(FPDF_FORMHANDLE form,
127 FPDF_PAGE page,
128 const std::vector<std::string>& tokens) {
129 if (tokens.size() < 5 && tokens.size() > 6) {
130 fprintf(stderr, "mousewheel: bad args\n");
131 return;
132 }
133
134 const FS_POINTF point = {static_cast<float>(atoi(tokens[1].c_str())),
135 static_cast<float>(atoi(tokens[2].c_str()))};
136 int delta_x = atoi(tokens[3].c_str());
137 int delta_y = atoi(tokens[4].c_str());
138 int modifiers = tokens.size() >= 6 ? GetModifiers(tokens[5]) : 0;
139 FORM_OnMouseWheel(form, page, modifiers, &point, delta_x, delta_y);
140 }
141
SendFocusEvent(FPDF_FORMHANDLE form,FPDF_PAGE page,const std::vector<std::string> & tokens)142 void SendFocusEvent(FPDF_FORMHANDLE form,
143 FPDF_PAGE page,
144 const std::vector<std::string>& tokens) {
145 if (tokens.size() != 3) {
146 fprintf(stderr, "focus: bad args\n");
147 return;
148 }
149
150 int x = atoi(tokens[1].c_str());
151 int y = atoi(tokens[2].c_str());
152 FORM_OnFocus(form, page, 0, x, y);
153 }
154
155 } // namespace
156
SendPageEvents(FPDF_FORMHANDLE form,FPDF_PAGE page,const std::string & events,const std::function<void ()> & idler)157 void SendPageEvents(FPDF_FORMHANDLE form,
158 FPDF_PAGE page,
159 const std::string& events,
160 const std::function<void()>& idler) {
161 auto lines = StringSplit(events, '\n');
162 for (const auto& line : lines) {
163 auto command = StringSplit(line, '#');
164 if (command[0].empty())
165 continue;
166 auto tokens = StringSplit(command[0], ',');
167 if (tokens[0] == "charcode") {
168 SendCharCodeEvent(form, page, tokens);
169 } else if (tokens[0] == "keycode") {
170 SendKeyCodeEvent(form, page, tokens);
171 } else if (tokens[0] == "mousedown") {
172 SendMouseDownEvent(form, page, tokens);
173 } else if (tokens[0] == "mouseup") {
174 SendMouseUpEvent(form, page, tokens);
175 } else if (tokens[0] == "mousedoubleclick") {
176 SendMouseDoubleClickEvent(form, page, tokens);
177 } else if (tokens[0] == "mousemove") {
178 SendMouseMoveEvent(form, page, tokens);
179 } else if (tokens[0] == "mousewheel") {
180 SendMouseWheelEvent(form, page, tokens);
181 } else if (tokens[0] == "focus") {
182 SendFocusEvent(form, page, tokens);
183 } else {
184 fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str());
185 }
186 idler();
187 }
188 }
189