private MLResult InternalGetThumbnail(long entryId, out Texture2D imageThumbnail)
        {
            imageThumbnail = null;

            if (!_watchHistory.ContainsKey(entryId))
            {
                return(MLResult.Create(MLResult.Code.InvalidParam, "Unknown entry Id"));
            }

            var thumbnail = new MLImageNativeBindings.MLImageNative();

            MLResult.Code resultCode = MLScreensNativeBindings.MLScreensGetWatchHistoryThumbnail(entryId, ref thumbnail);
            var           result     = MLResult.Create(resultCode);

            if (!result.IsOk)
            {
                return(result);
            }

            imageThumbnail =
                new Texture2D((int)thumbnail.Width,
                              (int)thumbnail.Height,
                              TextureFormat.RGB24,
                              false,
                              true);

            imageThumbnail.LoadRawTextureData(thumbnail.Image,
                                              (int)(thumbnail.Height * thumbnail.Width) * RGBBytesPerPixel);
            resultCode = MLScreensNativeBindings.MLScreensReleaseWatchHistoryThumbnail(ref thumbnail);
            result     = MLResult.Create(resultCode);
            return(result);
        }
        private MLResult InternalGetScreensInfo(out List <MLScreensScreenInfo> info)
        {
            info = new List <MLScreensScreenInfo>();
            _screenInfoList.Initialize();
            MLResult.Code resultCode = MLScreensNativeBindings.MLScreensGetScreenInfoListEx(ref _screenInfoList);
            var           result     = MLResult.Create(resultCode);

            if (!result.IsOk)
            {
                return(result);
            }

            long screensArrayAddress = _screenInfoList.Entries.ToInt64();

            for (var i = 0; i < _screenInfoList.Count; ++i)
            {
                long entryAddress = screensArrayAddress +
                                    i * Marshal.SizeOf(typeof(MLScreensNativeBindings.MLScreensScreenInfoExNative));
                var entryPtr    = new IntPtr(entryAddress);
                var entryNative = (MLScreensNativeBindings.MLScreensScreenInfoExNative)Marshal.PtrToStructure(entryPtr,
                                                                                                              typeof(MLScreensNativeBindings.MLScreensScreenInfoExNative));
                MLScreensScreenInfo entry = entryNative.Data;
                info.Add(entry);
            }
            resultCode = MLScreensNativeBindings.MLScreensReleaseScreenInfoListEx(ref _screenInfoList);
            result     = MLResult.Create(resultCode);
            return(result);
        }
        private MLResult InternalUpdateScreenInfo(MLScreensScreenInfo info)
        {
            MLScreensNativeBindings.MLScreensScreenInfoExNative nativeScreenInfo = MLScreensNativeBindings.MLScreensScreenInfoExNative.Create();
            nativeScreenInfo.Data = info;

            MLResult.Code resultCode = MLScreensNativeBindings.MLScreensUpdateScreenInfo(ref nativeScreenInfo);
            var           result     = MLResult.Create(resultCode);

            return(result);
        }
        private MLResult InternalRemove(long entryId)
        {
            if (!_watchHistory.ContainsKey(entryId))
            {
                return(MLResult.Create(MLResult.Code.InvalidParam, "Unknown entry Id"));
            }

            MLResult.Code resultCode = MLScreensNativeBindings.MLScreensRemoveWatchHistoryEntry(entryId);
            var           result     = MLResult.Create(resultCode);

            if (result.IsOk)
            {
                _watchHistory.Remove(entryId);
            }

            return(result);
        }
        private MLResult InternalGetScreensInfo(ulong id, out MLScreensScreenInfo screenInfo)
        {
            var screenNative = MLScreensNativeBindings.MLScreensScreenInfoExNative.Create();

            MLResult.Code resultCode = MLScreensNativeBindings.MLScreensGetScreenInfo(id, ref screenNative);
            var           result     = MLResult.Create(resultCode);

            if (!result.IsOk)
            {
                screenInfo = new MLScreensScreenInfo();
                return(result);
            }
            else
            {
                screenInfo = screenNative.Data;
            }

            return(result);
        }
        private MLResult InternalUpdateWatchHistory(MLScreensWatchHistoryEntry entry, Texture2D thumbnailImage)
        {
            if (!entry.IsValid)
            {
                return(MLResult.Create(MLResult.Code.InvalidParam, "Invalid entry parameter"));
            }

            if (!_watchHistory.ContainsKey(entry.Id))
            {
                return(MLResult.Create(MLResult.Code.InvalidParam, "Unknown entry Id"));
            }

            if (thumbnailImage != null && thumbnailImage.format != TextureFormat.RGB24)
            {
                return(MLResult.Create(MLResult.Code.InvalidParam, "Invalid thumbnail parameter format"));
            }

            MLScreensNativeBindings.MLScreensWatchHistoryEntryNative nativeEntry = MLScreensNativeBindings.MLScreensWatchHistoryEntryNative.Create();
            nativeEntry.Data = entry;

            MLImageNativeBindings.MLImageNative thumbnail =
                thumbnailImage == null ? _defaultGrayThumbnailImage : CreateThumbnailImage(thumbnailImage);
            MLResult.Code resultCode = MLScreensNativeBindings.MLScreensUpdateWatchHistoryEntry(ref nativeEntry, ref thumbnail);

            if (thumbnail.Image != IntPtr.Zero && thumbnail.Image != _defaultGrayThumbnailImage.Image)
            {
                Marshal.FreeHGlobal(thumbnail.Image);
            }

            Marshal.FreeHGlobal(nativeEntry.Title);
            Marshal.FreeHGlobal(nativeEntry.Subtitle);
            Marshal.FreeHGlobal(nativeEntry.CustomData);

            var result = MLResult.Create(resultCode);

            if (result.IsOk)
            {
                _watchHistory[entry.Id] = entry;
            }

            return(result);
        }
        /// <summary>
        /// Starts the screens object requests, Must be called to receive screens data from
        /// the underlying system
        /// </summary>
        protected override MLResult StartAPI()
        {
            MLResult.Code resultCode = MLScreensNativeBindings.MLScreensGetWatchHistoryList(ref _watchHistoryList);
            var           result     = MLResult.Create(resultCode);

            if (!result.IsOk)
            {
                MLPluginLog.ErrorFormat("MLScreens.StartAPI failed to retrieve saved screen information. Reason: {0}", result);
                return(result);
            }

            PopulateWatchHistory();
            resultCode = MLScreensNativeBindings.MLScreensReleaseWatchHistoryList(ref _watchHistoryList);
            result     = MLResult.Create(resultCode);
            if (!result.IsOk)
            {
                MLPluginLog.ErrorFormat("MLScreens.StartAPI failed to clean screens data. Reason: {0}", result);
                return(result);
            }

            _defaultGrayThumbnailImage = CreateGrayThumbnailImage();

            return(result);
        }
 /// <summary>
 /// Gets a readable version of the result code as an ASCII string.
 /// </summary>
 /// <param name="result">The MLResult that should be converted.</param>
 /// <returns>ASCII string containing a readable version of the result code.</returns>
 public static string GetResultString(MLResult.Code result)
 {
     return(Marshal.PtrToStringAnsi(MLScreensNativeBindings.MLScreensGetResultString(result)));
 }