mirror of
				https://github.com/swaywm/sway.git
				synced 2025-11-03 09:01:43 -05:00 
			
		
		
		
	use snprintf to get identifier len
This commit is contained in:
		
							parent
							
								
									9df4a2c7a8
								
							
						
					
					
						commit
						730af5e721
					
				
					 1 changed files with 2 additions and 14 deletions
				
			
		| 
						 | 
				
			
			@ -34,13 +34,6 @@ static struct sway_seat *input_manager_get_seat(
 | 
			
		|||
	return sway_seat_create(input, seat_name);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static inline int strlen_num(int num) {
 | 
			
		||||
	if (num == 0) {
 | 
			
		||||
		return 2;
 | 
			
		||||
	}
 | 
			
		||||
	return (int)((ceil(log10(abs(num)))+2));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static char *get_device_identifier(struct wlr_input_device *device) {
 | 
			
		||||
	int vendor = device->vendor;
 | 
			
		||||
	int product = device->product;
 | 
			
		||||
| 
						 | 
				
			
			@ -54,19 +47,14 @@ static char *get_device_identifier(struct wlr_input_device *device) {
 | 
			
		|||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	int len =
 | 
			
		||||
		(strlen(name) +
 | 
			
		||||
		strlen_num(device->vendor) +
 | 
			
		||||
		strlen_num(device->product) +
 | 
			
		||||
		3) * sizeof(char);
 | 
			
		||||
 | 
			
		||||
	const char *fmt = "%d:%d:%s";
 | 
			
		||||
	int len = snprintf(NULL, 0, fmt, vendor, product, name) + 1;
 | 
			
		||||
	char *identifier = malloc(len);
 | 
			
		||||
	if (!identifier) {
 | 
			
		||||
		sway_log(L_ERROR, "Unable to allocate unique input device name");
 | 
			
		||||
		return NULL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	const char *fmt = "%d:%d:%s";
 | 
			
		||||
	snprintf(identifier, len, fmt, vendor, product, name);
 | 
			
		||||
	free(name);
 | 
			
		||||
	return identifier;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue