Skip to content

Commit a08c819

Browse files
committed
Add inital Mac implementation of Usb + Keyboarding
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
1 parent bc8d560 commit a08c819

File tree

11 files changed

+240
-7
lines changed

11 files changed

+240
-7
lines changed

Palaso/Palaso.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@
176176
<None Include="..\lib\commonMono\NDesk.DBus.dll.config">
177177
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
178178
</None>
179+
<Reference Include="MonoMac" >
180+
<HintPath>..\lib\commonMono\MonoMac.dll</HintPath>
181+
</Reference>
182+
179183
</ItemGroup>
180184
<ItemGroup>
181185
<Reference Include="System.Configuration" />
@@ -355,6 +359,7 @@
355359
<Compile Include="UiBindings\ICountGiver.cs" />
356360
<Compile Include="UiBindings\IDisplayStringAdaptor.cs" />
357361
<Compile Include="UsbDrive\UsbDriveInfo.cs" />
362+
<Compile Include="UsbDrive\Mac\UsbDriveInfoMac.cs" />
358363
<Compile Include="IO\FileRelatedStrings.Designer.cs">
359364
<DependentUpon>FileRelatedStrings.resx</DependentUpon>
360365
<DesignTime>True</DesignTime>

Palaso/PalasoSetup.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public class PalasoSetup : IDisposable
1919
{
2020
public PalasoSetup()
2121
{
22+
if (Palaso.PlatformUtilities.Platform.IsMac)
23+
MonoMac.AppKit.NSApplication.Init();
24+
2225
}
2326
private bool disposed = false;
2427

@@ -40,7 +43,8 @@ protected virtual void Dispose(bool disposing)
4043
// program hang when closing. Closing the system bus allows the thread to close,
4144
// and thus the program to close. Closing the system bus can happen safely only
4245
// at the end of the program.
43-
NDesk.DBus.Bus.System.Close();
46+
if (Palaso.PlatformUtilities.Platform.IsLinux)
47+
NDesk.DBus.Bus.System.Close();
4448
#endif
4549
}
4650
disposed = true;
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#if __MonoCS__
2+
using System;
3+
using System.IO;
4+
using System.Collections.Generic;
5+
using MonoMac.Foundation;
6+
7+
namespace Palaso.UsbDrive.Mac
8+
{
9+
internal class UsbDriveInfoMac : UsbDriveInfo
10+
{
11+
private static readonly NSString NSURLIsVolumeKey = new NSString("NSURLIsVolumeKey");
12+
private static readonly NSString NSURLIsWritableKey = new NSString("NSURLIsWritableKey");
13+
private static readonly NSString NSURLVolumeLocalizedNameKey = new NSString("NSURLVolumeLocalizedNameKey");
14+
private static readonly NSString NSURLVolumeAvailableCapacityKey = new NSString("NSURLVolumeAvailableCapacityKey");
15+
private static readonly NSString NSURLVolumeTotalCapacityKey = new NSString("NSURLVolumeTotalCapacityKey");
16+
private static readonly NSString NSURLVolumeURLKey = new NSString("NSURLVolumeURLKey");
17+
private static readonly NSString NSURLVolumeIsRemovableKey = new NSString("NSURLVolumeIsRemovableKey");
18+
private static readonly NSString NSURLPathKey = new NSString("_NSURLPathKey");
19+
20+
private NSDictionary resourceValues;
21+
private NSUrl url;
22+
23+
private UsbDriveInfoMac()
24+
{
25+
}
26+
27+
public override bool IsReady
28+
{
29+
get { return true; }
30+
}
31+
32+
public override DirectoryInfo RootDirectory
33+
{
34+
get { return new DirectoryInfo(resourceValues[NSURLPathKey].ToString()); }
35+
}
36+
37+
public override string VolumeLabel
38+
{
39+
get { return resourceValues[NSURLVolumeLocalizedNameKey].ToString(); }
40+
}
41+
42+
public override ulong TotalSize
43+
{
44+
get { return (ulong) (NSNumber) resourceValues[NSURLVolumeTotalCapacityKey]; }
45+
}
46+
47+
public override ulong AvailableFreeSpace
48+
{
49+
get { return (ulong) (NSNumber) resourceValues[NSURLVolumeAvailableCapacityKey]; }
50+
}
51+
52+
public new static List<IUsbDriveInfo> GetDrives()
53+
{
54+
var drives = new List<IUsbDriveInfo>();
55+
56+
NSString[] keys = new NSString[] {
57+
NSURLIsVolumeKey,
58+
NSURLVolumeLocalizedNameKey,
59+
NSURLVolumeAvailableCapacityKey,
60+
NSURLVolumeTotalCapacityKey,
61+
NSURLVolumeIsRemovableKey,
62+
NSURLPathKey
63+
};
64+
65+
NSFileManager fm = NSFileManager.DefaultManager;
66+
var volumes = fm.GetMountedVolumes(NSArray.FromObjects(keys), NSVolumeEnumerationOptions.SkipHiddenVolumes);
67+
foreach (var url in volumes)
68+
{
69+
NSError error;
70+
var values = url.GetResourceValues(keys, out error);
71+
if ((bool) (NSNumber) values[NSURLVolumeIsRemovableKey])
72+
{
73+
var driveInfo = new UsbDriveInfoMac();
74+
driveInfo.resourceValues = values;
75+
driveInfo.url = url;
76+
drives.Add(driveInfo);
77+
}
78+
}
79+
80+
return drives;
81+
}
82+
}
83+
}
84+
#endif

Palaso/UsbDrive/UsbDriveInfo.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#if MONO
66
using Palaso.UsbDrive.Linux;
7+
using Palaso.UsbDrive.Mac;
78
#else
89
using Palaso.UsbDrive.Windows;
910
#endif
@@ -106,7 +107,10 @@ public abstract ulong TotalSize
106107
public static List<IUsbDriveInfo> GetDrives()
107108
{
108109
#if MONO
109-
return UsbDriveInfoUDisks.GetDrives(); // Lucid now uses UDisks, HAL use is deprecated.
110+
if (Palaso.PlatformUtilities.Platform.IsMac)
111+
return UsbDriveInfoMac.GetDrives();
112+
else
113+
return UsbDriveInfoUDisks.GetDrives(); // Lucid now uses UDisks, HAL use is deprecated.
110114
#else
111115
return UsbDriveInfoWindows.GetDrives();
112116
#endif

PalasoUIWindowsForms/Keyboarding/KeyboardController.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Palaso.UI.WindowsForms.Keyboarding.InternalInterfaces;
1414
#if __MonoCS__
1515
using Palaso.UI.WindowsForms.Keyboarding.Linux;
16+
using Palaso.UI.WindowsForms.Keyboarding.Mac;
1617
#else
1718
using Palaso.UI.WindowsForms.Keyboarding.Windows;
1819
#endif
@@ -63,14 +64,18 @@ public static void SetKeyboardAdaptors(IKeyboardAdaptor[] adaptors)
6364
/// </summary>
6465
public static void Reset()
6566
{
66-
SetKeyboardAdaptors(new IKeyboardAdaptor[] {
67+
SetKeyboardAdaptors(
6768
#if __MonoCS__
68-
new XkbKeyboardAdaptor(), new IbusKeyboardAdaptor(), new CombinedKeyboardAdaptor(),
69-
new CinnamonIbusAdaptor()
69+
(Palaso.PlatformUtilities.Platform.IsMac
70+
? new IKeyboardAdaptor[] { new MacKeyboardAdaptor() }
71+
72+
: new IKeyboardAdaptor[] { new XkbKeyboardAdaptor(), new IbusKeyboardAdaptor(),
73+
new CombinedKeyboardAdaptor(), new CinnamonIbusAdaptor() }
74+
)
7075
#else
71-
new WinKeyboardAdaptor(), new KeymanKeyboardAdaptor(),
76+
new IKeyboardAdaptor[] {new WinKeyboardAdaptor(), new KeymanKeyboardAdaptor() }
7277
#endif
73-
});
78+
);
7479
}
7580

7681
public static void InitializeAdaptors()
Binary file not shown.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#if __MonoCS__
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Windows.Input;
8+
using MonoMac.Foundation;
9+
using Palaso.UI.WindowsForms.Keyboarding.Interfaces;
10+
using Palaso.UI.WindowsForms.Keyboarding.InternalInterfaces;
11+
using Palaso.WritingSystems;
12+
13+
namespace Palaso.UI.WindowsForms.Keyboarding.Mac
14+
{
15+
internal class MacKeyboardAdaptor : IKeyboardAdaptor
16+
{
17+
protected List<IKeyboardErrorDescription> m_BadKeyboards;
18+
protected MonoMac.AppKit.NSTextInputContext m_Context;
19+
20+
public MacKeyboardAdaptor()
21+
{
22+
}
23+
24+
protected void InitKeyboards()
25+
{
26+
if (m_BadKeyboards != null)
27+
{
28+
return;
29+
}
30+
31+
ReinitKeyboards();
32+
}
33+
34+
private void ReinitKeyboards()
35+
{
36+
m_BadKeyboards = new List<IKeyboardErrorDescription>();
37+
m_Context = new MonoMac.AppKit.NSTextInputContext();
38+
var sources = NSArray.FromArray<NSString>(m_Context.KeyboardInputSources);
39+
foreach (var source in sources.Select(kis => kis.ToString()))
40+
{
41+
var localizedName = MonoMac.AppKit.NSTextInputContext.LocalizedNameForInputSource(source).ToString();
42+
var keyboard = new MacKeyboardDescription(source, localizedName, this);
43+
KeyboardController.Manager.RegisterKeyboard(keyboard);
44+
}
45+
}
46+
public void Initialize()
47+
{
48+
InitKeyboards();
49+
}
50+
51+
public void UpdateAvailableKeyboards()
52+
{
53+
ReinitKeyboards();
54+
}
55+
56+
public void Close()
57+
{
58+
m_Context = null;
59+
}
60+
61+
public List<IKeyboardErrorDescription> ErrorKeyboards { get; private set; }
62+
public bool ActivateKeyboard(IKeyboardDefinition keyboard)
63+
{
64+
Debug.Assert(keyboard is KeyboardDescription);
65+
Debug.Assert(((KeyboardDescription)keyboard).Engine == this);
66+
Debug.Assert(keyboard is MacKeyboardDescription);
67+
var cocoaKeyboard = keyboard as MacKeyboardDescription;
68+
m_Context.SelectedKeyboardInputSource = cocoaKeyboard.InputSource;
69+
70+
return true;
71+
}
72+
73+
public void DeactivateKeyboard(IKeyboardDefinition keyboard)
74+
{
75+
}
76+
77+
public IKeyboardDefinition GetKeyboardForInputLanguage(IInputLanguage inputLanguage)
78+
{
79+
throw new NotImplementedException();
80+
}
81+
82+
public IKeyboardDefinition CreateKeyboardDefinition(string layout, string locale)
83+
{
84+
throw new NotImplementedException();
85+
}
86+
87+
public IKeyboardDefinition ActiveKeyboard
88+
{
89+
get
90+
{
91+
var InputSource = m_Context.SelectedKeyboardInputSource;
92+
return Keyboard.Controller.AllAvailableKeyboards.OfType<MacKeyboardDescription>()
93+
.FirstOrDefault(macKeybd => macKeybd.InputSource == InputSource);
94+
}
95+
96+
}
97+
98+
public IKeyboardDefinition DefaultKeyboard { get; private set; }
99+
public KeyboardType Type { get { return KeyboardType.System;}}
100+
}
101+
}
102+
#endif
103+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#if __MonoCS__
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using Palaso.UI.WindowsForms.Keyboarding.InternalInterfaces;
7+
using Palaso.WritingSystems;
8+
9+
namespace Palaso.UI.WindowsForms.Keyboarding.Mac
10+
{
11+
internal class MacKeyboardDescription : KeyboardDescription
12+
{
13+
internal MacKeyboardDescription(string source, string localizedName, IKeyboardAdaptor engine) :
14+
base(localizedName, source, String.Empty, null, engine)
15+
{
16+
17+
}
18+
19+
public string InputSource { get { return Layout; } }
20+
}
21+
}
22+
#endif
23+

PalasoUIWindowsForms/PalasoUIWindowsForms.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@
266266
<None Include="..\lib\commonMono\NDesk.DBus.dll.config">
267267
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
268268
</None>
269+
<Reference Include="MonoMac">
270+
<HintPath>..\lib\commonMono\MonoMac.dll</HintPath>
271+
</Reference>
269272
</ItemGroup>
270273
<ItemGroup>
271274
<None Include="Progress\LogBoxSettings.settings">
@@ -465,6 +468,8 @@
465468
<Compile Include="Keyboarding\Linux\XkbKeyboardDescription.cs" />
466469
<Compile Include="Keyboarding\Linux\XklConfigRegistry.cs" />
467470
<Compile Include="Keyboarding\Linux\XklEngine.cs" />
471+
<Compile Include="Keyboarding\Mac\MacKeyboardDescription.cs" />
472+
<Compile Include="Keyboarding\Mac\MacKeyboardAdaptor.cs" />
468473
<Compile Include="Keyboarding\Types\InputLanguageWrapper.cs" />
469474
<Compile Include="Keyboarding\Types\KeyboardCollection.cs" />
470475
<Compile Include="Keyboarding\Types\MouseEvent.cs" />

lib/commonMono/MonoMac.dll

6.65 MB
Binary file not shown.

0 commit comments

Comments
 (0)