mirror of
				https://gitlab.freedesktop.org/wlroots/wlroots.git
				synced 2025-11-03 09:01:40 -05:00 
			
		
		
		
	Only allocate device state when it will be used
This design also avoids double frees in the future when we're creating several wlr_input_devices from one libinput_device.
This commit is contained in:
		
							parent
							
								
									7a5f35b5bb
								
							
						
					
					
						commit
						508d135de7
					
				
					 1 changed files with 4 additions and 3 deletions
				
			
		| 
						 | 
					@ -47,10 +47,12 @@ static void handle_device_added(struct wlr_backend_state *state,
 | 
				
			||||||
	const char *name = libinput_device_get_name(device);
 | 
						const char *name = libinput_device_get_name(device);
 | 
				
			||||||
	list_t *devices = list_create();
 | 
						list_t *devices = list_create();
 | 
				
			||||||
	wlr_log(L_DEBUG, "Added %s [%d:%d]", name, vendor, product);
 | 
						wlr_log(L_DEBUG, "Added %s [%d:%d]", name, vendor, product);
 | 
				
			||||||
	struct wlr_input_device_state *devstate =
 | 
					 | 
				
			||||||
		calloc(1, sizeof(struct wlr_input_device_state));
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD)) {
 | 
						if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD)) {
 | 
				
			||||||
 | 
							struct wlr_input_device_state *devstate =
 | 
				
			||||||
 | 
								calloc(1, sizeof(struct wlr_input_device_state));
 | 
				
			||||||
 | 
							devstate->handle = device;
 | 
				
			||||||
 | 
							libinput_device_ref(device);
 | 
				
			||||||
		struct wlr_input_device *wlr_device = wlr_input_device_create(
 | 
							struct wlr_input_device *wlr_device = wlr_input_device_create(
 | 
				
			||||||
			WLR_INPUT_DEVICE_KEYBOARD, &input_device_impl, devstate,
 | 
								WLR_INPUT_DEVICE_KEYBOARD, &input_device_impl, devstate,
 | 
				
			||||||
			name, vendor, product);
 | 
								name, vendor, product);
 | 
				
			||||||
| 
						 | 
					@ -80,7 +82,6 @@ static void handle_device_added(struct wlr_backend_state *state,
 | 
				
			||||||
	if (devices->length > 0) {
 | 
						if (devices->length > 0) {
 | 
				
			||||||
		libinput_device_set_user_data(device, devices);
 | 
							libinput_device_set_user_data(device, devices);
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		wlr_libinput_device_destroy(devstate);
 | 
					 | 
				
			||||||
		list_free(devices);
 | 
							list_free(devices);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue