]> gitweb.factorcode.org Git - factor.git/blob - extra/iokit/iokit.factor
Merge branch 'master' into experimental
[factor.git] / extra / iokit / iokit.factor
1 USING: alien.syntax alien.c-types core-foundation system
2 combinators kernel sequences debugger io accessors ;
3 IN: iokit
4
5 <<
6     os macosx?
7     [ "/System/Library/Frameworks/IOKit.framework" load-framework ]
8     when
9 >>
10
11 : kIOKitBuildVersionKey   "IOKitBuildVersion" ; inline
12 : kIOKitDiagnosticsKey   "IOKitDiagnostics" ; inline
13  
14 : kIORegistryPlanesKey   "IORegistryPlanes" ; inline
15 : kIOCatalogueKey    "IOCatalogue" ; inline
16
17 : kIOServicePlane    "IOService" ; inline
18 : kIOPowerPlane    "IOPower" ; inline
19 : kIODeviceTreePlane   "IODeviceTree" ; inline
20 : kIOAudioPlane    "IOAudio" ; inline
21 : kIOFireWirePlane   "IOFireWire" ; inline
22 : kIOUSBPlane    "IOUSB" ; inline
23
24 : kIOServiceClass    "IOService" ; inline
25
26 : kIOResourcesClass   "IOResources" ; inline
27
28 : kIOClassKey    "IOClass" ; inline
29 : kIOProbeScoreKey   "IOProbeScore" ; inline
30 : kIOKitDebugKey    "IOKitDebug" ; inline
31
32 : kIOProviderClassKey   "IOProviderClass" ; inline
33 : kIONameMatchKey    "IONameMatch" ; inline
34 : kIOPropertyMatchKey   "IOPropertyMatch" ; inline
35 : kIOPathMatchKey    "IOPathMatch" ; inline
36 : kIOLocationMatchKey   "IOLocationMatch" ; inline
37 : kIOParentMatchKey   "IOParentMatch" ; inline
38 : kIOResourceMatchKey   "IOResourceMatch" ; inline
39 : kIOMatchedServiceCountKey  "IOMatchedServiceCountMatch" ; inline
40
41 : kIONameMatchedKey   "IONameMatched" ; inline
42
43 : kIOMatchCategoryKey   "IOMatchCategory" ; inline
44 : kIODefaultMatchCategoryKey  "IODefaultMatchCategory" ; inline
45
46 : kIOUserClientClassKey   "IOUserClientClass" ; inline
47
48 : kIOUserClientCrossEndianKey   "IOUserClientCrossEndian" ; inline
49 : kIOUserClientCrossEndianCompatibleKey  "IOUserClientCrossEndianCompatible" ; inline
50 : kIOUserClientSharedInstanceKey   "IOUserClientSharedInstance" ; inline
51
52 : kIOPublishNotification   "IOServicePublish" ; inline
53 : kIOFirstPublishNotification  "IOServiceFirstPublish" ; inline
54 : kIOMatchedNotification   "IOServiceMatched" ; inline
55 : kIOFirstMatchNotification  "IOServiceFirstMatch" ; inline
56 : kIOTerminatedNotification  "IOServiceTerminate" ; inline
57
58 : kIOGeneralInterest   "IOGeneralInterest" ; inline
59 : kIOBusyInterest    "IOBusyInterest" ; inline
60 : kIOAppPowerStateInterest  "IOAppPowerStateInterest" ; inline
61 : kIOPriorityPowerStateInterest  "IOPriorityPowerStateInterest" ; inline
62
63 : kIOPlatformDeviceMessageKey "IOPlatformDeviceMessage" ; inline
64
65 : kIOCFPlugInTypesKey   "IOCFPlugInTypes" ; inline
66
67 : kIOCommandPoolSizeKey         "IOCommandPoolSize" ; inline
68
69 : kIOMaximumBlockCountReadKey "IOMaximumBlockCountRead" ; inline
70 : kIOMaximumBlockCountWriteKey "IOMaximumBlockCountWrite" ; inline
71 : kIOMaximumByteCountReadKey "IOMaximumByteCountRead" ; inline
72 : kIOMaximumByteCountWriteKey "IOMaximumByteCountWrite" ; inline
73 : kIOMaximumSegmentCountReadKey "IOMaximumSegmentCountRead" ; inline
74 : kIOMaximumSegmentCountWriteKey "IOMaximumSegmentCountWrite" ; inline
75 : kIOMaximumSegmentByteCountReadKey "IOMaximumSegmentByteCountRead" ; inline
76 : kIOMaximumSegmentByteCountWriteKey "IOMaximumSegmentByteCountWrite" ; inline
77 : kIOMinimumSegmentAlignmentByteCountKey "IOMinimumSegmentAlignmentByteCount" ; inline
78 : kIOMaximumSegmentAddressableBitCountKey "IOMaximumSegmentAddressableBitCount" ; inline
79
80 : kIOIconKey "IOIcon" ; inline
81 : kIOBundleResourceFileKey "IOBundleResourceFile" ; inline
82
83 : kIOBusBadgeKey "IOBusBadge" ; inline
84 : kIODeviceIconKey "IODeviceIcon" ; inline
85
86 : kIOPlatformSerialNumberKey  "IOPlatformSerialNumber"  ; inline
87
88 : kIOPlatformUUIDKey  "IOPlatformUUID"  ; inline
89
90 : kIONVRAMDeletePropertyKey  "IONVRAM-DELETE-PROPERTY" ; inline
91 : kIODTNVRAMPanicInfoKey   "aapl,panic-info" ; inline
92
93 : kIOBootDeviceKey "IOBootDevice"   ; inline
94 : kIOBootDevicePathKey "IOBootDevicePath"  ; inline
95 : kIOBootDeviceSizeKey "IOBootDeviceSize"  ; inline
96
97 : kOSBuildVersionKey   "OS Build Version" ; inline
98
99 : kNilOptions 0 ; inline
100
101 TYPEDEF: uint mach_port_t
102 TYPEDEF: int kern_return_t
103 TYPEDEF: int boolean_t
104 TYPEDEF: mach_port_t io_object_t
105 TYPEDEF: io_object_t io_iterator_t
106 TYPEDEF: io_object_t io_registry_entry_t
107 TYPEDEF: io_object_t io_service_t
108 TYPEDEF: char[128] io_name_t
109 TYPEDEF: char[512] io_string_t
110 TYPEDEF: kern_return_t IOReturn
111
112 TYPEDEF: uint IOOptionBits
113
114 : MACH_PORT_NULL 0 ; inline
115 : KERN_SUCCESS 0 ; inline
116
117 FUNCTION: IOReturn IOMasterPort ( mach_port_t bootstrap, mach_port_t* master ) ;
118
119 FUNCTION: CFDictionaryRef IOServiceMatching ( char* name ) ;
120 FUNCTION: CFDictionaryRef IOServiceNameMatching ( char* name ) ;
121 FUNCTION: CFDictionaryRef IOBSDNameMatching ( char* name ) ;
122
123 FUNCTION: IOReturn IOObjectRetain ( io_object_t o ) ;
124 FUNCTION: IOReturn IOObjectRelease ( io_object_t o ) ;
125
126 FUNCTION: IOReturn IOServiceGetMatchingServices ( mach_port_t master, CFDictionaryRef matchingDict, io_iterator_t* iterator ) ;
127
128 FUNCTION: io_object_t IOIteratorNext ( io_iterator_t i ) ;
129 FUNCTION: void IOIteratorReset ( io_iterator_t i ) ;
130 FUNCTION: boolean_t IOIteratorIsValid ( io_iterator_t i ) ;
131
132 FUNCTION: IOReturn IORegistryEntryGetPath ( io_registry_entry_t entry, io_name_t plane, io_string_t path ) ;
133
134 FUNCTION: IOReturn IORegistryEntryCreateCFProperties ( io_registry_entry_t entry, CFMutableDictionaryRef properties, CFAllocatorRef allocator, IOOptionBits options ) ;
135
136 FUNCTION: char* mach_error_string ( IOReturn error ) ;
137
138 TUPLE: mach-error error-code ;
139 C: <mach-error> mach-error
140
141 M: mach-error error.
142     "IOKit call failed: " print error-code>> mach_error_string print ;
143
144 : mach-error ( return -- )
145     dup KERN_SUCCESS = [ drop ] [ <mach-error> throw ] if ;
146
147 : master-port ( -- port )
148     MACH_PORT_NULL 0 <uint> [ IOMasterPort mach-error ] keep *uint ;
149
150 : io-services-matching-dictionary ( nsdictionary -- iterator )
151     master-port swap 0 <uint>
152     [ IOServiceGetMatchingServices mach-error ] keep
153     *uint ;
154
155 : io-services-matching-service ( service -- iterator )
156     IOServiceMatching io-services-matching-dictionary ;
157 : io-services-matching-service-name ( service-name -- iterator )
158     IOServiceNameMatching io-services-matching-dictionary ;
159 : io-services-matching-bsd-name ( bsd-name -- iterator )
160     IOBSDNameMatching io-services-matching-dictionary ;
161
162 : retain-io-object ( o -- o )
163     [ IOObjectRetain mach-error ] keep ;
164 : release-io-object ( o -- )
165     IOObjectRelease mach-error ;
166
167 : io-objects-from-iterator* ( i -- i array )
168     [ dup IOIteratorNext dup MACH_PORT_NULL = not ]
169     [ ]
170     [ drop ] produce ;
171
172 : io-objects-from-iterator ( i -- array )
173     io-objects-from-iterator* [ release-io-object ] dip ;
174     
175 : properties-from-io-object ( o -- o nsdictionary )
176     dup f <void*> [
177         kCFAllocatorDefault kNilOptions
178         IORegistryEntryCreateCFProperties mach-error
179     ]
180     keep *void* ;
181