| 
									
										
										
										
											2023-08-28 13:23:10 +02:00
										 |  |  | const { app, BrowserWindow } = require('electron/main') | 
					
						
							| 
									
										
										
										
											2022-11-22 16:50:32 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | function createWindow () { | 
					
						
							|  |  |  |   const mainWindow = new BrowserWindow({ | 
					
						
							|  |  |  |     width: 800, | 
					
						
							|  |  |  |     height: 600 | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2023-02-01 12:59:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-22 16:50:32 -05:00
										 |  |  |   let grantedDeviceThroughPermHandler | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   mainWindow.webContents.session.on('select-usb-device', (event, details, callback) => { | 
					
						
							| 
									
										
										
										
											2023-04-05 06:42:20 -07:00
										 |  |  |     // Add events to handle devices being added or removed before the callback on
 | 
					
						
							|  |  |  |     // `select-usb-device` is called.
 | 
					
						
							| 
									
										
										
										
											2023-02-01 12:59:16 +01:00
										 |  |  |     mainWindow.webContents.session.on('usb-device-added', (event, device) => { | 
					
						
							| 
									
										
										
										
											2022-11-22 16:50:32 -05:00
										 |  |  |       console.log('usb-device-added FIRED WITH', device) | 
					
						
							| 
									
										
										
										
											2023-04-05 06:42:20 -07:00
										 |  |  |       // Optionally update details.deviceList
 | 
					
						
							| 
									
										
										
										
											2022-11-22 16:50:32 -05:00
										 |  |  |     }) | 
					
						
							| 
									
										
										
										
											2023-02-01 12:59:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-22 16:50:32 -05:00
										 |  |  |     mainWindow.webContents.session.on('usb-device-removed', (event, device) => { | 
					
						
							|  |  |  |       console.log('usb-device-removed FIRED WITH', device) | 
					
						
							| 
									
										
										
										
											2023-04-05 06:42:20 -07:00
										 |  |  |       // Optionally update details.deviceList
 | 
					
						
							| 
									
										
										
										
											2022-11-22 16:50:32 -05:00
										 |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     event.preventDefault() | 
					
						
							|  |  |  |     if (details.deviceList && details.deviceList.length > 0) { | 
					
						
							| 
									
										
										
										
											2023-04-05 06:42:20 -07:00
										 |  |  |       const deviceToReturn = details.deviceList.find((device) => { | 
					
						
							| 
									
										
										
										
											2023-05-15 00:58:35 -07:00
										 |  |  |         return !grantedDeviceThroughPermHandler || (device.deviceId !== grantedDeviceThroughPermHandler.deviceId) | 
					
						
							| 
									
										
										
										
											2022-11-22 16:50:32 -05:00
										 |  |  |       }) | 
					
						
							|  |  |  |       if (deviceToReturn) { | 
					
						
							| 
									
										
										
										
											2023-02-01 12:59:16 +01:00
										 |  |  |         callback(deviceToReturn.deviceId) | 
					
						
							| 
									
										
										
										
											2022-11-22 16:50:32 -05:00
										 |  |  |       } else { | 
					
						
							|  |  |  |         callback() | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   mainWindow.webContents.session.setPermissionCheckHandler((webContents, permission, requestingOrigin, details) => { | 
					
						
							|  |  |  |     if (permission === 'usb' && details.securityOrigin === 'file:///') { | 
					
						
							|  |  |  |       return true | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   mainWindow.webContents.session.setDevicePermissionHandler((details) => { | 
					
						
							|  |  |  |     if (details.deviceType === 'usb' && details.origin === 'file://') { | 
					
						
							| 
									
										
										
										
											2023-02-01 12:59:16 +01:00
										 |  |  |       if (!grantedDeviceThroughPermHandler) { | 
					
						
							| 
									
										
										
										
											2022-11-22 16:50:32 -05:00
										 |  |  |         grantedDeviceThroughPermHandler = details.device | 
					
						
							|  |  |  |         return true | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         return false | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2023-02-01 12:59:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-30 12:35:25 -04:00
										 |  |  |   mainWindow.webContents.session.setUSBProtectedClassesHandler((details) => { | 
					
						
							|  |  |  |     return details.protectedClasses.filter((usbClass) => { | 
					
						
							|  |  |  |       // Exclude classes except for audio classes
 | 
					
						
							|  |  |  |       return usbClass.indexOf('audio') === -1 | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-22 16:50:32 -05:00
										 |  |  |   mainWindow.loadFile('index.html') | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | app.whenReady().then(() => { | 
					
						
							|  |  |  |   createWindow() | 
					
						
							| 
									
										
										
										
											2023-02-01 12:59:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-22 16:50:32 -05:00
										 |  |  |   app.on('activate', function () { | 
					
						
							|  |  |  |     if (BrowserWindow.getAllWindows().length === 0) createWindow() | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | app.on('window-all-closed', function () { | 
					
						
							|  |  |  |   if (process.platform !== 'darwin') app.quit() | 
					
						
							|  |  |  | }) |