|
| 1 | +// Copyright 2026 The Cobalt Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "starboard/tvos/shared/platform_service.h" |
| 16 | + |
| 17 | +#import <UIKit/UIKit.h> |
| 18 | + |
| 19 | +#include "starboard/common/log.h" |
| 20 | +#include "starboard/common/string.h" |
| 21 | +#include "starboard/extension/platform_service.h" |
| 22 | +#import "starboard/tvos/shared/starboard_application.h" |
| 23 | + |
| 24 | +typedef struct CobaltExtensionPlatformServicePrivate { |
| 25 | + void* Send(void* /*data*/, |
| 26 | + uint64_t /*length*/, |
| 27 | + uint64_t* output_length, |
| 28 | + bool* invalid_state) { |
| 29 | + if (!output_length || !invalid_state) { |
| 30 | + return NULL; |
| 31 | + } |
| 32 | + |
| 33 | + *invalid_state = false; |
| 34 | + std::string string = starboard::FormatString( |
| 35 | + "MaximumFramesPerSecond:%ld;DisplayRefreshRate:%f;", |
| 36 | + SBDGetApplication().maximumFramesPerSecond, |
| 37 | + SBDGetApplication().displayRefreshRate); |
| 38 | + |
| 39 | + *output_length = string.size() + 1; |
| 40 | + char* output = (char*)malloc(*output_length); |
| 41 | + if (!output) { |
| 42 | + *invalid_state = true; |
| 43 | + return NULL; |
| 44 | + } |
| 45 | + |
| 46 | + memcpy(output, string.c_str(), *output_length); |
| 47 | + return output; // caller must free() |
| 48 | + } |
| 49 | + |
| 50 | + void* context; |
| 51 | + ReceiveMessageCallback receive_callback; |
| 52 | +} CobaltExtensionPlatformServicePrivate; |
| 53 | + |
| 54 | +namespace starboard { |
| 55 | +namespace shared { |
| 56 | + |
| 57 | +namespace { |
| 58 | + |
| 59 | +const char* kClientLogInfoServiceName = "dev.cobalt.coat.clientloginfo"; |
| 60 | + |
| 61 | +bool Has(const char* name) { |
| 62 | + if (!name) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + if (strcmp(name, kClientLogInfoServiceName) == 0) { |
| 67 | + return true; |
| 68 | + } |
| 69 | + |
| 70 | + return false; |
| 71 | +} |
| 72 | + |
| 73 | +CobaltExtensionPlatformService Open(void* context, |
| 74 | + const char* name, |
| 75 | + ReceiveMessageCallback receive_callback) { |
| 76 | + SB_DCHECK(context); |
| 77 | + |
| 78 | + if (!Has(name)) { |
| 79 | + SB_LOG(ERROR) << "Can't open Service " << name; |
| 80 | + return kCobaltExtensionPlatformServiceInvalid; |
| 81 | + } |
| 82 | + |
| 83 | + CobaltExtensionPlatformService service = |
| 84 | + new CobaltExtensionPlatformServicePrivate({context, receive_callback}); |
| 85 | + return service; |
| 86 | +} |
| 87 | + |
| 88 | +void Close(CobaltExtensionPlatformService service) { |
| 89 | + if (service == kCobaltExtensionPlatformServiceInvalid) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + delete static_cast<CobaltExtensionPlatformServicePrivate*>(service); |
| 94 | +} |
| 95 | + |
| 96 | +void* Send(CobaltExtensionPlatformService service, |
| 97 | + void* data, |
| 98 | + uint64_t length, |
| 99 | + uint64_t* output_length, |
| 100 | + bool* invalid_state) { |
| 101 | + SB_DCHECK(output_length); |
| 102 | + SB_DCHECK(invalid_state); |
| 103 | + |
| 104 | + return static_cast<CobaltExtensionPlatformServicePrivate*>(service)->Send( |
| 105 | + data, length, output_length, invalid_state); |
| 106 | +} |
| 107 | + |
| 108 | +const CobaltExtensionPlatformServiceApi kPlatformServiceApi = { |
| 109 | + kCobaltExtensionPlatformServiceName, |
| 110 | + 1, // API version that's implemented. |
| 111 | + &Has, |
| 112 | + &Open, |
| 113 | + &Close, |
| 114 | + &Send}; |
| 115 | + |
| 116 | +} // namespace |
| 117 | + |
| 118 | +const void* GetPlatformServiceApi() { |
| 119 | + return &kPlatformServiceApi; |
| 120 | +} |
| 121 | + |
| 122 | +} // namespace shared |
| 123 | +} // namespace starboard |
0 commit comments