-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[mono] Implement System.Console for iOS (#33827)
* Add iOS version of System.Console
- Loading branch information
Showing
9 changed files
with
284 additions
and
27 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
src/libraries/Common/src/Interop/OSX/System.Native/Interop.Log.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class Sys | ||
{ | ||
[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_Log")] | ||
internal static extern unsafe bool Log(byte* buffer, int count); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
#pragma once | ||
|
||
#include "pal_compiler.h" | ||
#include "pal_types.h" | ||
|
||
PALEXPORT void SystemNative_Log(uint8_t* buffer, int32_t length); | ||
|
||
// Called by pal_signal.cpp to reinitialize the console on SIGCONT/SIGCHLD. | ||
void ReinitializeTerminal(void) {} | ||
void UninitializeTerminal(void) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
#include "pal_log.h" | ||
#import <Foundation/Foundation.h> | ||
|
||
void SystemNative_Log (uint8_t* buffer, int32_t length) | ||
{ | ||
NSString *msg = [[NSString alloc] initWithBytes: buffer length: length encoding: NSUTF16LittleEndianStringEncoding]; | ||
if (length > 4096) | ||
{ | ||
// Write in chunks of max 4096 characters; older versions of iOS seems to have a bug where NSLog may hang with long strings (!). | ||
// https://github.com/xamarin/maccore/issues/1014 | ||
const char* utf8 = [msg UTF8String]; | ||
size_t len = utf8 == NULL ? 0 : strlen (utf8); | ||
const size_t max_size = 4096; | ||
while (len > 0) | ||
{ | ||
size_t chunk_size = len > max_size ? max_size : len; | ||
|
||
// Try to not break in the middle of a line, by looking backwards for a newline | ||
while (chunk_size > 0 && utf8 [chunk_size] != 0 && utf8 [chunk_size] != '\n') | ||
{ | ||
chunk_size--; | ||
} | ||
if (chunk_size == 0) | ||
{ | ||
// No newline found, break in the middle. | ||
chunk_size = len > max_size ? max_size : len; | ||
} | ||
NSLog (@"%.*s", (int) chunk_size, utf8); | ||
len -= chunk_size; | ||
utf8 += chunk_size; | ||
} | ||
} | ||
else | ||
{ | ||
NSLog (@"%@", msg); | ||
} | ||
[msg release]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
src/libraries/System.Console/src/System/ConsolePal.iOS.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.IO; | ||
using System.Text; | ||
|
||
namespace System | ||
{ | ||
internal sealed class NSLogStream : ConsoleStream | ||
{ | ||
public NSLogStream() : base(FileAccess.Write) {} | ||
|
||
public override int Read(byte[] buffer, int offset, int count) => throw Error.GetReadNotSupported(); | ||
|
||
public override unsafe void Write(byte[] buffer, int offset, int count) | ||
{ | ||
ValidateWrite(buffer, offset, count); | ||
|
||
fixed (byte* ptr = buffer) | ||
{ | ||
Interop.Sys.Log(ptr + offset, count); | ||
} | ||
} | ||
} | ||
|
||
internal static class ConsolePal | ||
{ | ||
public static Stream OpenStandardInput() => throw new PlatformNotSupportedException(); | ||
|
||
public static Stream OpenStandardOutput() => new NSLogStream(); | ||
|
||
public static Stream OpenStandardError() => new NSLogStream(); | ||
|
||
public static Encoding InputEncoding => throw new PlatformNotSupportedException(); | ||
|
||
public static void SetConsoleInputEncoding(Encoding enc) => throw new PlatformNotSupportedException(); | ||
|
||
public static Encoding OutputEncoding => Encoding.Unicode; | ||
|
||
// underlying API expects only utf-16 | ||
public static void SetConsoleOutputEncoding(Encoding enc) => throw new PlatformNotSupportedException(); | ||
|
||
public static bool IsInputRedirectedCore() => false; | ||
|
||
public static bool IsOutputRedirectedCore() => false; | ||
|
||
public static bool IsErrorRedirectedCore() => false; | ||
|
||
internal static TextReader GetOrCreateReader() => throw new PlatformNotSupportedException(); | ||
|
||
public static bool NumberLock => false; | ||
|
||
public static bool CapsLock => false; | ||
|
||
public static bool KeyAvailable => false; | ||
|
||
public static ConsoleKeyInfo ReadKey(bool intercept) => throw new PlatformNotSupportedException(); | ||
|
||
public static bool TreatControlCAsInput | ||
{ | ||
get => throw new PlatformNotSupportedException(); | ||
set => throw new PlatformNotSupportedException(); | ||
} | ||
|
||
public static ConsoleColor BackgroundColor | ||
{ | ||
get => throw new PlatformNotSupportedException(); | ||
set => throw new PlatformNotSupportedException(); | ||
} | ||
|
||
public static ConsoleColor ForegroundColor | ||
{ | ||
get => throw new PlatformNotSupportedException(); | ||
set => throw new PlatformNotSupportedException(); | ||
} | ||
|
||
public static void ResetColor() => throw new PlatformNotSupportedException(); | ||
|
||
public static int CursorSize | ||
{ | ||
get => throw new PlatformNotSupportedException(); | ||
set => throw new PlatformNotSupportedException(); | ||
} | ||
|
||
public static bool CursorVisible | ||
{ | ||
get => throw new PlatformNotSupportedException(); | ||
set => throw new PlatformNotSupportedException(); | ||
} | ||
|
||
public static int CursorLeft => throw new PlatformNotSupportedException(); | ||
|
||
public static int CursorTop => throw new PlatformNotSupportedException(); | ||
|
||
public static string Title | ||
{ | ||
get => throw new PlatformNotSupportedException(); | ||
set => throw new PlatformNotSupportedException(); | ||
} | ||
|
||
public static void Beep() => throw new PlatformNotSupportedException(); | ||
|
||
public static void Beep(int frequency, int duration) => throw new PlatformNotSupportedException(); | ||
|
||
public static void MoveBufferArea(int sourceLeft, int sourceTop, | ||
int sourceWidth, int sourceHeight, int targetLeft, int targetTop, | ||
char sourceChar, ConsoleColor sourceForeColor, | ||
ConsoleColor sourceBackColor) => throw new PlatformNotSupportedException(); | ||
|
||
public static void Clear() => throw new PlatformNotSupportedException(); | ||
|
||
public static void SetCursorPosition(int left, int top) => throw new PlatformNotSupportedException(); | ||
|
||
public static int BufferWidth | ||
{ | ||
get => throw new PlatformNotSupportedException(); | ||
set => throw new PlatformNotSupportedException(); | ||
} | ||
|
||
public static int BufferHeight | ||
{ | ||
get => throw new PlatformNotSupportedException(); | ||
set => throw new PlatformNotSupportedException(); | ||
} | ||
|
||
public static void SetBufferSize(int width, int height) => throw new PlatformNotSupportedException(); | ||
|
||
public static int LargestWindowWidth => throw new PlatformNotSupportedException(); | ||
|
||
public static int LargestWindowHeight => throw new PlatformNotSupportedException(); | ||
|
||
public static int WindowLeft | ||
{ | ||
get => throw new PlatformNotSupportedException(); | ||
set => throw new PlatformNotSupportedException(); | ||
} | ||
|
||
public static int WindowTop | ||
{ | ||
get => throw new PlatformNotSupportedException(); | ||
set => throw new PlatformNotSupportedException(); | ||
} | ||
|
||
public static int WindowWidth | ||
{ | ||
get => throw new PlatformNotSupportedException(); | ||
set => throw new PlatformNotSupportedException(); | ||
} | ||
|
||
public static int WindowHeight | ||
{ | ||
get => throw new PlatformNotSupportedException(); | ||
set => throw new PlatformNotSupportedException(); | ||
} | ||
|
||
public static void SetWindowPosition(int left, int top) => throw new PlatformNotSupportedException(); | ||
|
||
public static void SetWindowSize(int width, int height) => throw new PlatformNotSupportedException(); | ||
|
||
internal sealed class ControlCHandlerRegistrar | ||
{ | ||
internal ControlCHandlerRegistrar() => throw new PlatformNotSupportedException(); | ||
|
||
internal void Register() => throw new PlatformNotSupportedException(); | ||
|
||
internal void Unregister() => throw new PlatformNotSupportedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.