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