Skip to content

Commit

Permalink
Add inital Mac implementation of Usb + Keyboarding
Browse files Browse the repository at this point in the history
These are not well tested.

This version of MonoMac.dll includes additional methods for
KeyboardInputSources and SelectedKeyboardInputSource for
NSTextInputContext.

chrisvire/monomac@b3cbfd2
mono/monomac#129
  • Loading branch information
chrisvire committed Mar 5, 2015
1 parent bc8d560 commit a08c819
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 7 deletions.
5 changes: 5 additions & 0 deletions Palaso/Palaso.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@
<None Include="..\lib\commonMono\NDesk.DBus.dll.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Reference Include="MonoMac" >
<HintPath>..\lib\commonMono\MonoMac.dll</HintPath>
</Reference>

</ItemGroup>
<ItemGroup>
<Reference Include="System.Configuration" />
Expand Down Expand Up @@ -355,6 +359,7 @@
<Compile Include="UiBindings\ICountGiver.cs" />
<Compile Include="UiBindings\IDisplayStringAdaptor.cs" />
<Compile Include="UsbDrive\UsbDriveInfo.cs" />
<Compile Include="UsbDrive\Mac\UsbDriveInfoMac.cs" />
<Compile Include="IO\FileRelatedStrings.Designer.cs">
<DependentUpon>FileRelatedStrings.resx</DependentUpon>
<DesignTime>True</DesignTime>
Expand Down
6 changes: 5 additions & 1 deletion Palaso/PalasoSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class PalasoSetup : IDisposable
{
public PalasoSetup()
{
if (Palaso.PlatformUtilities.Platform.IsMac)
MonoMac.AppKit.NSApplication.Init();

}
private bool disposed = false;

Expand All @@ -40,7 +43,8 @@ protected virtual void Dispose(bool disposing)
// program hang when closing. Closing the system bus allows the thread to close,
// and thus the program to close. Closing the system bus can happen safely only
// at the end of the program.
NDesk.DBus.Bus.System.Close();
if (Palaso.PlatformUtilities.Platform.IsLinux)
NDesk.DBus.Bus.System.Close();
#endif
}
disposed = true;
Expand Down
84 changes: 84 additions & 0 deletions Palaso/UsbDrive/Mac/UsbDriveInfoMac.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#if __MonoCS__
using System;
using System.IO;
using System.Collections.Generic;
using MonoMac.Foundation;

namespace Palaso.UsbDrive.Mac
{
internal class UsbDriveInfoMac : UsbDriveInfo
{
private static readonly NSString NSURLIsVolumeKey = new NSString("NSURLIsVolumeKey");
private static readonly NSString NSURLIsWritableKey = new NSString("NSURLIsWritableKey");
private static readonly NSString NSURLVolumeLocalizedNameKey = new NSString("NSURLVolumeLocalizedNameKey");
private static readonly NSString NSURLVolumeAvailableCapacityKey = new NSString("NSURLVolumeAvailableCapacityKey");
private static readonly NSString NSURLVolumeTotalCapacityKey = new NSString("NSURLVolumeTotalCapacityKey");
private static readonly NSString NSURLVolumeURLKey = new NSString("NSURLVolumeURLKey");
private static readonly NSString NSURLVolumeIsRemovableKey = new NSString("NSURLVolumeIsRemovableKey");
private static readonly NSString NSURLPathKey = new NSString("_NSURLPathKey");

private NSDictionary resourceValues;
private NSUrl url;

private UsbDriveInfoMac()
{
}

public override bool IsReady
{
get { return true; }
}

public override DirectoryInfo RootDirectory
{
get { return new DirectoryInfo(resourceValues[NSURLPathKey].ToString()); }
}

public override string VolumeLabel
{
get { return resourceValues[NSURLVolumeLocalizedNameKey].ToString(); }
}

public override ulong TotalSize
{
get { return (ulong) (NSNumber) resourceValues[NSURLVolumeTotalCapacityKey]; }
}

public override ulong AvailableFreeSpace
{
get { return (ulong) (NSNumber) resourceValues[NSURLVolumeAvailableCapacityKey]; }
}

public new static List<IUsbDriveInfo> GetDrives()
{
var drives = new List<IUsbDriveInfo>();

NSString[] keys = new NSString[] {
NSURLIsVolumeKey,
NSURLVolumeLocalizedNameKey,
NSURLVolumeAvailableCapacityKey,
NSURLVolumeTotalCapacityKey,
NSURLVolumeIsRemovableKey,
NSURLPathKey
};

NSFileManager fm = NSFileManager.DefaultManager;
var volumes = fm.GetMountedVolumes(NSArray.FromObjects(keys), NSVolumeEnumerationOptions.SkipHiddenVolumes);
foreach (var url in volumes)
{
NSError error;
var values = url.GetResourceValues(keys, out error);
if ((bool) (NSNumber) values[NSURLVolumeIsRemovableKey])
{
var driveInfo = new UsbDriveInfoMac();
driveInfo.resourceValues = values;
driveInfo.url = url;
drives.Add(driveInfo);
}
}

return drives;
}
}
}
#endif
6 changes: 5 additions & 1 deletion Palaso/UsbDrive/UsbDriveInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#if MONO
using Palaso.UsbDrive.Linux;
using Palaso.UsbDrive.Mac;
#else
using Palaso.UsbDrive.Windows;
#endif
Expand Down Expand Up @@ -106,7 +107,10 @@ public abstract ulong TotalSize
public static List<IUsbDriveInfo> GetDrives()
{
#if MONO
return UsbDriveInfoUDisks.GetDrives(); // Lucid now uses UDisks, HAL use is deprecated.
if (Palaso.PlatformUtilities.Platform.IsMac)
return UsbDriveInfoMac.GetDrives();
else
return UsbDriveInfoUDisks.GetDrives(); // Lucid now uses UDisks, HAL use is deprecated.
#else
return UsbDriveInfoWindows.GetDrives();
#endif
Expand Down
15 changes: 10 additions & 5 deletions PalasoUIWindowsForms/Keyboarding/KeyboardController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Palaso.UI.WindowsForms.Keyboarding.InternalInterfaces;
#if __MonoCS__
using Palaso.UI.WindowsForms.Keyboarding.Linux;
using Palaso.UI.WindowsForms.Keyboarding.Mac;
#else
using Palaso.UI.WindowsForms.Keyboarding.Windows;
#endif
Expand Down Expand Up @@ -63,14 +64,18 @@ public static void SetKeyboardAdaptors(IKeyboardAdaptor[] adaptors)
/// </summary>
public static void Reset()
{
SetKeyboardAdaptors(new IKeyboardAdaptor[] {
SetKeyboardAdaptors(
#if __MonoCS__
new XkbKeyboardAdaptor(), new IbusKeyboardAdaptor(), new CombinedKeyboardAdaptor(),
new CinnamonIbusAdaptor()
(Palaso.PlatformUtilities.Platform.IsMac
? new IKeyboardAdaptor[] { new MacKeyboardAdaptor() }

: new IKeyboardAdaptor[] { new XkbKeyboardAdaptor(), new IbusKeyboardAdaptor(),
new CombinedKeyboardAdaptor(), new CinnamonIbusAdaptor() }
)
#else
new WinKeyboardAdaptor(), new KeymanKeyboardAdaptor(),
new IKeyboardAdaptor[] {new WinKeyboardAdaptor(), new KeymanKeyboardAdaptor() }
#endif
});
);
}

public static void InitializeAdaptors()
Expand Down
Binary file not shown.
103 changes: 103 additions & 0 deletions PalasoUIWindowsForms/Keyboarding/Mac/MacKeyboardAdaptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#if __MonoCS__
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Input;
using MonoMac.Foundation;
using Palaso.UI.WindowsForms.Keyboarding.Interfaces;
using Palaso.UI.WindowsForms.Keyboarding.InternalInterfaces;
using Palaso.WritingSystems;

namespace Palaso.UI.WindowsForms.Keyboarding.Mac
{
internal class MacKeyboardAdaptor : IKeyboardAdaptor
{
protected List<IKeyboardErrorDescription> m_BadKeyboards;
protected MonoMac.AppKit.NSTextInputContext m_Context;

public MacKeyboardAdaptor()
{
}

protected void InitKeyboards()
{
if (m_BadKeyboards != null)
{
return;
}

ReinitKeyboards();
}

private void ReinitKeyboards()
{
m_BadKeyboards = new List<IKeyboardErrorDescription>();
m_Context = new MonoMac.AppKit.NSTextInputContext();
var sources = NSArray.FromArray<NSString>(m_Context.KeyboardInputSources);
foreach (var source in sources.Select(kis => kis.ToString()))
{
var localizedName = MonoMac.AppKit.NSTextInputContext.LocalizedNameForInputSource(source).ToString();
var keyboard = new MacKeyboardDescription(source, localizedName, this);
KeyboardController.Manager.RegisterKeyboard(keyboard);
}
}
public void Initialize()
{
InitKeyboards();
}

public void UpdateAvailableKeyboards()
{
ReinitKeyboards();
}

public void Close()
{
m_Context = null;
}

public List<IKeyboardErrorDescription> ErrorKeyboards { get; private set; }
public bool ActivateKeyboard(IKeyboardDefinition keyboard)
{
Debug.Assert(keyboard is KeyboardDescription);
Debug.Assert(((KeyboardDescription)keyboard).Engine == this);
Debug.Assert(keyboard is MacKeyboardDescription);
var cocoaKeyboard = keyboard as MacKeyboardDescription;
m_Context.SelectedKeyboardInputSource = cocoaKeyboard.InputSource;

return true;
}

public void DeactivateKeyboard(IKeyboardDefinition keyboard)
{
}

public IKeyboardDefinition GetKeyboardForInputLanguage(IInputLanguage inputLanguage)
{
throw new NotImplementedException();
}

public IKeyboardDefinition CreateKeyboardDefinition(string layout, string locale)
{
throw new NotImplementedException();
}

public IKeyboardDefinition ActiveKeyboard
{
get
{
var InputSource = m_Context.SelectedKeyboardInputSource;
return Keyboard.Controller.AllAvailableKeyboards.OfType<MacKeyboardDescription>()
.FirstOrDefault(macKeybd => macKeybd.InputSource == InputSource);
}

}

public IKeyboardDefinition DefaultKeyboard { get; private set; }
public KeyboardType Type { get { return KeyboardType.System;}}
}
}
#endif

23 changes: 23 additions & 0 deletions PalasoUIWindowsForms/Keyboarding/Mac/MacKeyboardDescription.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#if __MonoCS__
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Palaso.UI.WindowsForms.Keyboarding.InternalInterfaces;
using Palaso.WritingSystems;

namespace Palaso.UI.WindowsForms.Keyboarding.Mac
{
internal class MacKeyboardDescription : KeyboardDescription
{
internal MacKeyboardDescription(string source, string localizedName, IKeyboardAdaptor engine) :
base(localizedName, source, String.Empty, null, engine)
{

}

public string InputSource { get { return Layout; } }
}
}
#endif

5 changes: 5 additions & 0 deletions PalasoUIWindowsForms/PalasoUIWindowsForms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@
<None Include="..\lib\commonMono\NDesk.DBus.dll.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Reference Include="MonoMac">
<HintPath>..\lib\commonMono\MonoMac.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="Progress\LogBoxSettings.settings">
Expand Down Expand Up @@ -465,6 +468,8 @@
<Compile Include="Keyboarding\Linux\XkbKeyboardDescription.cs" />
<Compile Include="Keyboarding\Linux\XklConfigRegistry.cs" />
<Compile Include="Keyboarding\Linux\XklEngine.cs" />
<Compile Include="Keyboarding\Mac\MacKeyboardDescription.cs" />
<Compile Include="Keyboarding\Mac\MacKeyboardAdaptor.cs" />
<Compile Include="Keyboarding\Types\InputLanguageWrapper.cs" />
<Compile Include="Keyboarding\Types\KeyboardCollection.cs" />
<Compile Include="Keyboarding\Types\MouseEvent.cs" />
Expand Down
Binary file added lib/commonMono/MonoMac.dll
Binary file not shown.
Binary file added lib/commonMono/MonoMac.dll.mdb
Binary file not shown.

0 comments on commit a08c819

Please sign in to comment.