|
| 1 | +# coding=utf8 |
| 2 | +# @PydevCodeAnalysisIgnore |
| 3 | +# This file is part of Xpra. |
| 4 | +# Copyright (C) 2017 Antoine Martin <[email protected]> |
| 5 | +# Xpra is released under the terms of the GNU GPL v2, or, at your option, any |
| 6 | +# later version. See the file COPYING for details. |
| 7 | + |
| 8 | +# This file is based on this gist: |
| 9 | +# https://gist.github.com/pudquick/cff1ecdc02b4cabe5aa0dc6919d97c6d |
| 10 | + |
| 11 | + |
| 12 | +from Foundation import NSBundle |
| 13 | +import objc |
| 14 | + |
| 15 | +def _get_keyboard_layouts(): |
| 16 | + HIToolbox_bundle = NSBundle.bundleWithIdentifier_("com.apple.HIToolbox") |
| 17 | + HIT_functions = [ |
| 18 | + ('TISCreateInputSourceList','@@B'), |
| 19 | + ('TISGetInputSourceProperty', '@@@'), |
| 20 | + ] |
| 21 | + |
| 22 | + HIT_constants = [ |
| 23 | + ('kTISPropertyInputSourceType', '@'), |
| 24 | + ('kTISTypeKeyboardLayout', '@'), |
| 25 | + ('kTISTypeKeyboardInputMode', '@'), |
| 26 | + ('kTISPropertyLocalizedName', '@'), |
| 27 | + ('kTISPropertyBundleID', '@'), |
| 28 | + ('kTISPropertyInputModeID', '@'), |
| 29 | + ('kTISPropertyInputSourceID', '@'), |
| 30 | + ('kTISPropertyLocale', '@'), |
| 31 | + ('kTISPropertyKeyLayoutNumber', '@'), |
| 32 | + ('kTISPropertyScriptCode', '@'), |
| 33 | + ] |
| 34 | + |
| 35 | + objc.loadBundleFunctions(HIToolbox_bundle, globals(), HIT_functions) |
| 36 | + # Yes, I know it's amusing that loadBundleVariables is loading constants - ... oh, so it's just me then? k |
| 37 | + objc.loadBundleVariables(HIToolbox_bundle, globals(), HIT_constants) |
| 38 | + #silence pydev: |
| 39 | + |
| 40 | + # get the list of keyboard layouts |
| 41 | + keyboard_layouts = TISCreateInputSourceList({kTISPropertyInputSourceType: kTISTypeKeyboardLayout}, True) |
| 42 | + # ( |
| 43 | + # "<TSMInputSource 0x7ff8e9f2cc00> KB Layout: U.S. (id=0)", |
| 44 | + # "<TSMInputSource 0x7ff8e9f2c5f0> KB Layout: Czech - QWERTY (id=30778)", |
| 45 | + # "<TSMInputSource 0x7ff8e9f2b8c0> KB Layout: Czech (id=30776)", |
| 46 | + # [...] |
| 47 | + print("TISCreateInputSourceList keyboard_layouts=%s", keyboard_layouts) |
| 48 | + return keyboard_layouts |
| 49 | + |
| 50 | +def _get_keyboard_layout_dict(layout): |
| 51 | + def getprop(k): |
| 52 | + return TISGetInputSourceProperty(layout, k) |
| 53 | + # Can't figure out the constant names for these ones, so we'll make 'em up |
| 54 | + kTISPropertyKind = u'TSMInputSourcePropertyKind' |
| 55 | + # <TSMInputSource 0x7ff8e9f2c5f0> KB Layout: Czech - QWERTY (id=30778) |
| 56 | + kind = getprop(kTISPropertyKind) |
| 57 | + # u'TSMInputSourceKindKeyboardLayout' |
| 58 | + name = getprop(kTISPropertyLocalizedName) |
| 59 | + # u'Czech - QWERTY' |
| 60 | + kid = getprop(kTISPropertyKeyLayoutNumber) |
| 61 | + # 30778 |
| 62 | + script = getprop(kTISPropertyScriptCode) |
| 63 | + # 29 |
| 64 | + sid = getprop(kTISPropertyInputSourceID) |
| 65 | + # u'com.apple.keylayout.Czech-QWERTY' |
| 66 | + locale = getprop(kTISPropertyLocale) |
| 67 | + # u'cs' |
| 68 | + return { |
| 69 | + "kind" : kind, |
| 70 | + "name" : name, |
| 71 | + "id" : kid, |
| 72 | + "script" : script, |
| 73 | + "sid" : sid, |
| 74 | + "locale" : locale, |
| 75 | + } |
| 76 | + |
| 77 | +def get_keyboard_layout(): |
| 78 | + layouts = _get_keyboard_layouts() |
| 79 | + if not layouts: |
| 80 | + return {} |
| 81 | + #assume the first layout is the active one... |
| 82 | + return _get_keyboard_layout_dict(layouts[0]) |
| 83 | + |
| 84 | +def get_keyboard_layouts(): |
| 85 | + return [_get_keyboard_layout_dict(layout) for layout in _get_keyboard_layouts()] |
0 commit comments