1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "AnimationParser.h" 18 19 #include <android-base/stringprintf.h> 20 #include <android-base/strings.h> 21 22 #include <cutils/klog.h> 23 24 #include "animation.h" 25 26 #define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0) 27 #define LOGW(x...) do { KLOG_WARNING("charger", x); } while (0) 28 #define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0) 29 30 namespace android { 31 32 // Lines consisting of only whitespace or whitespace followed by '#' can be ignored. 33 bool can_ignore_line(const char* str) { 34 for (int i = 0; str[i] != '\0' && str[i] != '#'; i++) { 35 if (!isspace(str[i])) return false; 36 } 37 return true; 38 } 39 40 bool remove_prefix(const std::string& line, const char* prefix, const char** rest) { 41 const char* str = line.c_str(); 42 int start; 43 char c; 44 45 std::string format = base::StringPrintf(" %s%%n%%c", prefix); 46 if (sscanf(str, format.c_str(), &start, &c) != 1) { 47 return false; 48 } 49 50 *rest = &str[start]; 51 return true; 52 } 53 54 bool parse_text_field(const char* in, animation::text_field* field) { 55 int* x = &field->pos_x; 56 int* y = &field->pos_y; 57 int* r = &field->color_r; 58 int* g = &field->color_g; 59 int* b = &field->color_b; 60 int* a = &field->color_a; 61 62 int start = 0, end = 0; 63 64 if (sscanf(in, "c c %d %d %d %d %n%*s%n", r, g, b, a, &start, &end) == 4) { 65 *x = CENTER_VAL; 66 *y = CENTER_VAL; 67 } else if (sscanf(in, "c %d %d %d %d %d %n%*s%n", y, r, g, b, a, &start, &end) == 5) { 68 *x = CENTER_VAL; 69 } else if (sscanf(in, "%d c %d %d %d %d %n%*s%n", x, r, g, b, a, &start, &end) == 5) { 70 *y = CENTER_VAL; 71 } else if (sscanf(in, "%d %d %d %d %d %d %n%*s%n", x, y, r, g, b, a, &start, &end) != 6) { 72 return false; 73 } 74 75 if (end == 0) return false; 76 77 field->font_file.assign(&in[start], end - start); 78 79 return true; 80 } 81 82 bool parse_animation_desc(const std::string& content, animation* anim) { 83 static constexpr const char* animation_prefix = "animation: "; 84 static constexpr const char* fail_prefix = "fail: "; 85 static constexpr const char* clock_prefix = "clock_display: "; 86 static constexpr const char* percent_prefix = "percent_display: "; 87 static constexpr const char* frame_prefix = "frame: "; 88 89 std::vector<animation::frame> frames; 90 91 for (const auto& line : base::Split(content, "\n")) { 92 animation::frame frame; 93 const char* rest; 94 95 if (can_ignore_line(line.c_str())) { 96 continue; 97 } else if (remove_prefix(line, animation_prefix, &rest)) { 98 int start = 0, end = 0; 99 if (sscanf(rest, "%d %d %n%*s%n", &anim->num_cycles, &anim->first_frame_repeats, 100 &start, &end) != 2 || 101 end == 0) { 102 LOGE("Bad animation format: %s\n", line.c_str()); 103 return false; 104 } else { 105 anim->animation_file.assign(&rest[start], end - start); 106 } 107 } else if (remove_prefix(line, fail_prefix, &rest)) { 108 anim->fail_file.assign(rest); 109 } else if (remove_prefix(line, clock_prefix, &rest)) { 110 if (!parse_text_field(rest, &anim->text_clock)) { 111 LOGE("Bad clock_display format: %s\n", line.c_str()); 112 return false; 113 } 114 } else if (remove_prefix(line, percent_prefix, &rest)) { 115 if (!parse_text_field(rest, &anim->text_percent)) { 116 LOGE("Bad percent_display format: %s\n", line.c_str()); 117 return false; 118 } 119 } else if (sscanf(line.c_str(), " frame: %d %d %d", 120 &frame.disp_time, &frame.min_level, &frame.max_level) == 3) { 121 frames.push_back(std::move(frame)); 122 } else { 123 LOGE("Malformed animation description line: %s\n", line.c_str()); 124 return false; 125 } 126 } 127 128 if (anim->animation_file.empty() || frames.empty()) { 129 LOGE("Bad animation description. Provide the 'animation: ' line and at least one 'frame: ' " 130 "line.\n"); 131 return false; 132 } 133 134 anim->num_frames = frames.size(); 135 anim->frames = new animation::frame[frames.size()]; 136 std::copy(frames.begin(), frames.end(), anim->frames); 137 138 return true; 139 } 140 141 } // namespace android 142