Dan Sinclair | cb87682 | 2018-03-22 16:21:04 +0000 | [diff] [blame] | 1 | // Copyright 2018 The PDFium Authors. All rights reserved. |
| 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> |
Henrique Nakashima | 08b6819 | 2018-06-26 15:28:28 +0000 | [diff] [blame] | 10 | #include <vector> |
Dan Sinclair | cb87682 | 2018-03-22 16:21:04 +0000 | [diff] [blame] | 11 | |
Henrique Nakashima | 08b6819 | 2018-06-26 15:28:28 +0000 | [diff] [blame] | 12 | #include "public/fpdf_fwlevent.h" |
Dan Sinclair | cb87682 | 2018-03-22 16:21:04 +0000 | [diff] [blame] | 13 | #include "public/fpdfview.h" |
Lei Zhang | b6992dd | 2019-02-05 23:30:20 +0000 | [diff] [blame] | 14 | #include "testing/fx_string_testhelpers.h" |
Dan Sinclair | cb87682 | 2018-03-22 16:21:04 +0000 | [diff] [blame] | 15 | |
Henrique Nakashima | 08b6819 | 2018-06-26 15:28:28 +0000 | [diff] [blame] | 16 | namespace { |
| 17 | void SendCharCodeEvent(FPDF_FORMHANDLE form, |
| 18 | FPDF_PAGE page, |
| 19 | const std::vector<std::string>& tokens) { |
| 20 | if (tokens.size() != 2) { |
| 21 | fprintf(stderr, "charcode: bad args\n"); |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | int keycode = atoi(tokens[1].c_str()); |
| 26 | FORM_OnChar(form, page, keycode, 0); |
| 27 | } |
| 28 | |
| 29 | void SendKeyCodeEvent(FPDF_FORMHANDLE form, |
| 30 | FPDF_PAGE page, |
| 31 | const std::vector<std::string>& tokens) { |
| 32 | if (tokens.size() != 2) { |
| 33 | fprintf(stderr, "keycode: bad args\n"); |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | int keycode = atoi(tokens[1].c_str()); |
| 38 | FORM_OnKeyDown(form, page, keycode, 0); |
| 39 | FORM_OnKeyUp(form, page, keycode, 0); |
| 40 | } |
| 41 | |
| 42 | uint32_t GetModifiers(std::string modifiers_string) { |
| 43 | int modifiers = 0; |
| 44 | if (modifiers_string.find("shift") != std::string::npos) |
| 45 | modifiers |= FWL_EVENTFLAG_ShiftKey; |
| 46 | if (modifiers_string.find("control") != std::string::npos) |
| 47 | modifiers |= FWL_EVENTFLAG_ControlKey; |
| 48 | if (modifiers_string.find("alt") != std::string::npos) |
| 49 | modifiers |= FWL_EVENTFLAG_AltKey; |
| 50 | |
| 51 | return modifiers; |
| 52 | } |
| 53 | |
| 54 | void SendMouseDownEvent(FPDF_FORMHANDLE form, |
| 55 | FPDF_PAGE page, |
| 56 | const std::vector<std::string>& tokens) { |
| 57 | if (tokens.size() != 4 && tokens.size() != 5) { |
| 58 | fprintf(stderr, "mousedown: bad args\n"); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | int x = atoi(tokens[2].c_str()); |
| 63 | int y = atoi(tokens[3].c_str()); |
| 64 | uint32_t modifiers = tokens.size() >= 5 ? GetModifiers(tokens[4]) : 0; |
| 65 | |
| 66 | if (tokens[1] == "left") |
| 67 | FORM_OnLButtonDown(form, page, modifiers, x, y); |
| 68 | #ifdef PDF_ENABLE_XFA |
| 69 | else if (tokens[1] == "right") |
| 70 | FORM_OnRButtonDown(form, page, modifiers, x, y); |
| 71 | #endif |
| 72 | else |
| 73 | fprintf(stderr, "mousedown: bad button name\n"); |
| 74 | } |
| 75 | |
| 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 | #ifdef PDF_ENABLE_XFA |
| 90 | else if (tokens[1] == "right") |
| 91 | FORM_OnRButtonUp(form, page, modifiers, x, y); |
| 92 | #endif |
| 93 | else |
| 94 | fprintf(stderr, "mouseup: bad button name\n"); |
| 95 | } |
| 96 | |
| 97 | void SendMouseMoveEvent(FPDF_FORMHANDLE form, |
| 98 | FPDF_PAGE page, |
| 99 | const std::vector<std::string>& tokens) { |
| 100 | if (tokens.size() != 3) { |
| 101 | fprintf(stderr, "mousemove: bad args\n"); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | int x = atoi(tokens[1].c_str()); |
| 106 | int y = atoi(tokens[2].c_str()); |
| 107 | FORM_OnMouseMove(form, page, 0, x, y); |
| 108 | } |
| 109 | |
| 110 | void SendFocusEvent(FPDF_FORMHANDLE form, |
| 111 | FPDF_PAGE page, |
| 112 | const std::vector<std::string>& tokens) { |
| 113 | if (tokens.size() != 3) { |
| 114 | fprintf(stderr, "focus: bad args\n"); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | int x = atoi(tokens[1].c_str()); |
| 119 | int y = atoi(tokens[2].c_str()); |
| 120 | FORM_OnFocus(form, page, 0, x, y); |
| 121 | } |
| 122 | } // namespace |
| 123 | |
Dan Sinclair | cb87682 | 2018-03-22 16:21:04 +0000 | [diff] [blame] | 124 | void SendPageEvents(FPDF_FORMHANDLE form, |
| 125 | FPDF_PAGE page, |
| 126 | const std::string& events) { |
| 127 | auto lines = StringSplit(events, '\n'); |
| 128 | for (auto line : lines) { |
| 129 | auto command = StringSplit(line, '#'); |
| 130 | if (command[0].empty()) |
| 131 | continue; |
| 132 | auto tokens = StringSplit(command[0], ','); |
| 133 | if (tokens[0] == "charcode") { |
Henrique Nakashima | 08b6819 | 2018-06-26 15:28:28 +0000 | [diff] [blame] | 134 | SendCharCodeEvent(form, page, tokens); |
Dan Sinclair | cb87682 | 2018-03-22 16:21:04 +0000 | [diff] [blame] | 135 | } else if (tokens[0] == "keycode") { |
Henrique Nakashima | 08b6819 | 2018-06-26 15:28:28 +0000 | [diff] [blame] | 136 | SendKeyCodeEvent(form, page, tokens); |
Dan Sinclair | cb87682 | 2018-03-22 16:21:04 +0000 | [diff] [blame] | 137 | } else if (tokens[0] == "mousedown") { |
Henrique Nakashima | 08b6819 | 2018-06-26 15:28:28 +0000 | [diff] [blame] | 138 | SendMouseDownEvent(form, page, tokens); |
Dan Sinclair | cb87682 | 2018-03-22 16:21:04 +0000 | [diff] [blame] | 139 | } else if (tokens[0] == "mouseup") { |
Henrique Nakashima | 08b6819 | 2018-06-26 15:28:28 +0000 | [diff] [blame] | 140 | SendMouseUpEvent(form, page, tokens); |
Dan Sinclair | cb87682 | 2018-03-22 16:21:04 +0000 | [diff] [blame] | 141 | } else if (tokens[0] == "mousemove") { |
Henrique Nakashima | 08b6819 | 2018-06-26 15:28:28 +0000 | [diff] [blame] | 142 | SendMouseMoveEvent(form, page, tokens); |
Dan Sinclair | cb87682 | 2018-03-22 16:21:04 +0000 | [diff] [blame] | 143 | } else if (tokens[0] == "focus") { |
Henrique Nakashima | 08b6819 | 2018-06-26 15:28:28 +0000 | [diff] [blame] | 144 | SendFocusEvent(form, page, tokens); |
Dan Sinclair | cb87682 | 2018-03-22 16:21:04 +0000 | [diff] [blame] | 145 | } else { |
| 146 | fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str()); |
| 147 | } |
| 148 | } |
| 149 | } |