├── .editorconfig ├── .gitattributes ├── .gitignore ├── Package.swift ├── Sources └── IsCameraOn │ └── IsCameraOn.swift ├── license └── readme.md /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.build 2 | /Packages 3 | /*.xcodeproj 4 | /.swiftpm 5 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.9 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "IsCameraOn", 6 | platforms: [ 7 | .macOS(.v10_13) 8 | ], 9 | products: [ 10 | .library( 11 | name: "IsCameraOn", 12 | targets: [ 13 | "IsCameraOn" 14 | ] 15 | ) 16 | ], 17 | targets: [ 18 | .target( 19 | name: "IsCameraOn" 20 | ) 21 | ] 22 | ) 23 | -------------------------------------------------------------------------------- /Sources/IsCameraOn/IsCameraOn.swift: -------------------------------------------------------------------------------- 1 | import CoreMediaIO 2 | 3 | private func getCameraProp() -> CMIOObjectID? { 4 | var opa = CMIOObjectPropertyAddress( 5 | mSelector: CMIOObjectPropertySelector(kCMIOHardwarePropertyDevices), 6 | mScope: CMIOObjectPropertyScope(kCMIOObjectPropertyScopeGlobal), 7 | mElement: CMIOObjectPropertyElement(kCMIOObjectPropertyElementMaster) 8 | ) 9 | 10 | var dataSize: UInt32 = 0 11 | var dataUsed: UInt32 = 0 12 | var result = CMIOObjectGetPropertyDataSize(CMIOObjectID(kCMIOObjectSystemObject), &opa, 0, nil, &dataSize) 13 | var devices: UnsafeMutableRawPointer? 14 | 15 | defer { 16 | if let devices { 17 | free(devices) 18 | } 19 | } 20 | 21 | repeat { 22 | if let devicesStrong = devices { 23 | free(devicesStrong) 24 | devices = nil 25 | } 26 | 27 | devices = malloc(Int(dataSize)) 28 | result = CMIOObjectGetPropertyData(CMIOObjectID(kCMIOObjectSystemObject), &opa, 0, nil, dataSize, &dataUsed, devices) 29 | } while result == OSStatus(kCMIOHardwareBadPropertySizeError) 30 | 31 | var cameraId: CMIOObjectID? 32 | 33 | if let devices { 34 | for offset in stride(from: 0, to: dataSize, by: MemoryLayout.size) { 35 | let current = devices.advanced(by: Int(offset)).assumingMemoryBound(to: CMIOObjectID.self) 36 | cameraId = current.pointee 37 | } 38 | } 39 | 40 | return cameraId 41 | } 42 | 43 | public func isCameraOn() -> Bool { 44 | guard let camera = getCameraProp() else { 45 | return false 46 | } 47 | 48 | var opa = CMIOObjectPropertyAddress( 49 | mSelector: CMIOObjectPropertySelector(kCMIODevicePropertyDeviceIsRunningSomewhere), 50 | mScope: CMIOObjectPropertyScope(kCMIOObjectPropertyScopeWildcard), 51 | mElement: CMIOObjectPropertyElement(kCMIOObjectPropertyElementWildcard) 52 | ) 53 | 54 | var isUsed = false 55 | 56 | var dataSize: UInt32 = 0 57 | var dataUsed: UInt32 = 0 58 | var result = CMIOObjectGetPropertyDataSize(camera, &opa, 0, nil, &dataSize) 59 | if result == OSStatus(kCMIOHardwareNoError) { 60 | if let data = malloc(Int(dataSize)) { 61 | result = CMIOObjectGetPropertyData(camera, &opa, 0, nil, dataSize, &dataUsed, data) 62 | let on = data.assumingMemoryBound(to: UInt8.self) 63 | isUsed = on.pointee != 0 64 | } 65 | } 66 | 67 | return isUsed 68 | } 69 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # is-camera-on 2 | 3 | > Check if the built-in Mac camera is on 4 | 5 | The camera is commonly known as FaceTime HD or iSight. 6 | 7 | This module can be useful to check if the camera is already in use or notify you if it's turned on when you didn't intend it to be. 8 | 9 | ## Requirements 10 | 11 | macOS 10.13+ 12 | 13 | ## Install 14 | 15 | Add the following to `Package.swift`: 16 | 17 | ```swift 18 | .package(url: "https://github.com/sindresorhus/is-camera-on", from: "2.0.2") 19 | ``` 20 | 21 | [Or add the package in Xcode.](https://developer.apple.com/documentation/xcode/adding_package_dependencies_to_your_app) 22 | 23 | ## Usage 24 | 25 | ```swift 26 | import IsCameraOn 27 | 28 | print(isCameraOn()) 29 | //=> true 30 | ``` 31 | 32 | ## Related 33 | 34 | - [node-is-camera-on](https://github.com/sindresorhus/node-is-camera-on) - Node.js wrapper for this module 35 | - [is-camera-on-cli](https://github.com/sindresorhus/is-camera-on-cli) - CLI for this module 36 | - [macos-wallpaper](https://github.com/sindresorhus/macos-wallpaper) - Manage the desktop wallpaper 37 | - [do-not-disturb](https://github.com/sindresorhus/do-not-disturb) - Control the macOS `Do Not Disturb` feature 38 | - [More…](https://github.com/search?q=user%3Asindresorhus+language%3Aswift+archived%3Afalse&type=repositories) 39 | --------------------------------------------------------------------------------