mirror of
				https://gitlab.freedesktop.org/wlroots/wlroots.git
				synced 2025-10-29 05:40:12 -04:00 
			
		
		
		
	 8589ae19de
			
		
	
	
		8589ae19de
		
	
	
	
	
		
			
			A few pedantic changes and unused variables (1-4), and genuine bugs (5, 6). The reports with the corresponding files and lines numbers are as follows. 1. backend/libinput/tablet_pad.c@31,44,57 "Allocator sizeof operand mismatch" "Result of 'calloc' is converted to a pointer of type 'unsigned int', which is incompatible with sizeof operand type 'int'" 2. types/tablet_v2/wlr_tablet_v2_pad.c@371 "Allocator sizeof operand mismatch" "Result of 'calloc' is converted to a pointer of type 'uint32_t', which is incompatible with sizeof operand type 'int'" 3. types/wlr_cursor.c@335 "Dead initialization" "Value stored to 'dx'/'dy' during its initialization is never read" 4. rootston/xdg_shell.c@510 "Dead initialization" "Value stored to 'desktop' during its initialization is never read" 5. types/tablet_v2/wlr_tablet_v2_pad.c@475 "Dereference of null pointer" "Access to field 'strips' results in a dereference of a null pointer (loaded from field 'current_client')" The boolean logic was incorrect (c.f. the check in the following function). 6. examples/idle.c@163,174,182 "Uninitialized argument value" "1st function call argument is an uninitialized value" If close_timeout != 0, but simulate_activity_timeout >= close_timeout, the program would segfault at pthread_cancel(t1).
		
			
				
	
	
		
			198 lines
		
	
	
	
		
			4.7 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			198 lines
		
	
	
	
		
			4.7 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| #include <getopt.h>
 | |
| #include <pthread.h>
 | |
| #include <stdio.h>
 | |
| #include <stdlib.h>
 | |
| #include <string.h>
 | |
| #include <unistd.h>
 | |
| #include <wayland-client-protocol.h>
 | |
| #include <wayland-client.h>
 | |
| #include <wlr/util/log.h>
 | |
| #include "idle-client-protocol.h"
 | |
| 
 | |
| static struct org_kde_kwin_idle *idle_manager = NULL;
 | |
| static struct wl_seat *seat = NULL;
 | |
| static uint32_t timeout = 0, simulate_activity_timeout = 0, close_timeout = 0;
 | |
| static int run = 1;
 | |
| 
 | |
| struct thread_args {
 | |
| 	struct wl_display *display;
 | |
| 	struct org_kde_kwin_idle_timeout *timer;
 | |
| };
 | |
| 
 | |
| static void handle_global(void *data, struct wl_registry *registry,
 | |
| 		uint32_t name, const char *interface, uint32_t version) {
 | |
| 	fprintf(stdout, "interfaces found: %s\n", interface);
 | |
| 	if (strcmp(interface, "org_kde_kwin_idle") == 0) {
 | |
| 		idle_manager = wl_registry_bind(registry, name, &org_kde_kwin_idle_interface, 1);
 | |
| 	}
 | |
| 	else if (strcmp(interface, "wl_seat") == 0) {
 | |
| 		seat = wl_registry_bind(registry, name, &wl_seat_interface, 1);
 | |
| 	}
 | |
| }
 | |
| 
 | |
| static void handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) {
 | |
| 	//TODO
 | |
| }
 | |
| 
 | |
| static const struct wl_registry_listener registry_listener = {
 | |
| 	.global = handle_global,
 | |
| 	.global_remove = handle_global_remove,
 | |
| };
 | |
| 
 | |
| static void handle_idle(void* data, struct org_kde_kwin_idle_timeout *timer) {
 | |
| 	fprintf(stdout, "idle state\n");
 | |
| }
 | |
| 
 | |
| static void handle_resume(void* data, struct org_kde_kwin_idle_timeout *timer) {
 | |
| 	fprintf(stdout, "active state\n");
 | |
| }
 | |
| 
 | |
| static const struct org_kde_kwin_idle_timeout_listener idle_timer_listener = {
 | |
| 	.idle = handle_idle,
 | |
| 	.resumed = handle_resume,
 | |
| };
 | |
| 
 | |
| int parse_args(int argc, char *argv[]) {
 | |
| 	int c;
 | |
| 	while ((c = getopt(argc, argv, "c:hs:t:")) != -1) {
 | |
| 		switch(c)
 | |
| 		{
 | |
| 			case 'c':
 | |
| 				close_timeout = strtoul(optarg, NULL, 10);
 | |
| 				break;
 | |
| 			case 's':
 | |
| 				simulate_activity_timeout = strtoul(optarg, NULL, 10);
 | |
| 				break;
 | |
| 			case 't':
 | |
| 				timeout = strtoul(optarg, NULL, 10);
 | |
| 				break;
 | |
| 			case 'h':
 | |
| 			case '?':
 | |
| 				printf("Usage: %s [OPTIONS]\n", argv[0]);
 | |
| 				printf("  -t seconds\t\t\tidle timeout in seconds\n");
 | |
| 				printf("  -s seconds optional\t\tsimulate user activity after x seconds\n");
 | |
| 				printf("  -c seconds optional\t\tclose program after x seconds\n");
 | |
| 				printf("  -h\t\t\t\tthis help menu\n");
 | |
| 				return 1;
 | |
| 			default:
 | |
| 				return 1;
 | |
| 		}
 | |
| 	}
 | |
| 	return 0;
 | |
| }
 | |
| 
 | |
| void *simulate_activity(void *data) {
 | |
| 	sleep(simulate_activity_timeout);
 | |
| 	fprintf(stdout, "simulate user activity\n");
 | |
| 	struct thread_args *arg = data;
 | |
| 	org_kde_kwin_idle_timeout_simulate_user_activity(arg->timer);
 | |
| 	wl_display_roundtrip(arg->display);
 | |
| 	return NULL;
 | |
| }
 | |
| 
 | |
| void *close_program(void *data) {
 | |
| 	sleep(close_timeout);
 | |
| 	struct thread_args *arg = data;
 | |
| 	org_kde_kwin_idle_timeout_release(arg->timer);
 | |
| 	wl_display_roundtrip(arg->display);
 | |
| 	fprintf(stdout, "close program\n");
 | |
| 	run = 0;
 | |
| 	return NULL;
 | |
| }
 | |
| 
 | |
| void *main_loop(void *data) {
 | |
| 	struct wl_display *display = data;
 | |
| 	while (wl_display_dispatch(display) != -1) {
 | |
| 		;
 | |
| 	}
 | |
| 	return NULL;
 | |
| }
 | |
| 
 | |
| int main(int argc, char *argv[]) {
 | |
| 	wlr_log_init(WLR_DEBUG, NULL);
 | |
| 
 | |
| 	if (parse_args(argc, argv) != 0) {
 | |
| 		return -1;
 | |
| 	}
 | |
| 	if (timeout == 0) {
 | |
| 		printf("idle timeout 0 is invalid\n");
 | |
| 		return -1;
 | |
| 	}
 | |
| 
 | |
| 	struct wl_display *display = wl_display_connect(NULL);
 | |
| 	if (display == NULL) {
 | |
| 		fprintf(stderr, "failed to create display\n");
 | |
| 		return -1;
 | |
| 	}
 | |
| 
 | |
| 	struct wl_registry *registry = wl_display_get_registry(display);
 | |
| 	wl_registry_add_listener(registry, ®istry_listener, NULL);
 | |
| 	wl_display_dispatch(display);
 | |
| 	wl_display_roundtrip(display);
 | |
| 
 | |
| 	if (idle_manager == NULL) {
 | |
| 		fprintf(stderr, "display doesn't support idle protocol\n");
 | |
| 		return -1;
 | |
| 	}
 | |
| 	if (seat== NULL) {
 | |
| 		fprintf(stderr, "seat error\n");
 | |
| 		return -1;
 | |
| 	}
 | |
| 	struct org_kde_kwin_idle_timeout *timer =
 | |
| 		org_kde_kwin_idle_get_idle_timeout(idle_manager, seat, timeout * 1000);
 | |
| 
 | |
| 	if (timer == NULL) {
 | |
| 		fprintf(stderr, "Could not create idle_timeout\n");
 | |
| 		return -1;
 | |
| 	}
 | |
| 
 | |
| 	pthread_t t1, t2, t3;
 | |
| 	struct thread_args arg = {
 | |
| 		.timer = timer,
 | |
| 		.display = display,
 | |
| 	};
 | |
| 
 | |
| 	bool create_t1 = (simulate_activity_timeout != 0) &&
 | |
| 	                (simulate_activity_timeout < close_timeout);
 | |
| 
 | |
| 	if (create_t1) {
 | |
| 		if (pthread_create(&t1, NULL, &simulate_activity, (void *)&arg) != 0) {
 | |
| 			return -1;
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	bool create_t2 = (close_timeout != 0);
 | |
| 
 | |
| 	if (create_t2) {
 | |
| 		if (pthread_create(&t2, NULL, &close_program, (void *)&arg) != 0) {
 | |
| 			if (create_t1) {
 | |
| 				pthread_cancel(t1);
 | |
| 			}
 | |
| 			return -1;
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	org_kde_kwin_idle_timeout_add_listener(timer, &idle_timer_listener, timer);
 | |
| 	fprintf(stdout, "waiting\n");
 | |
| 
 | |
| 	if (pthread_create(&t3, NULL, &main_loop, (void *)display) != 0) {
 | |
| 		if (create_t1) {
 | |
| 			pthread_cancel(t1);
 | |
| 		}
 | |
| 		if (create_t2) {
 | |
| 			pthread_cancel(t2);
 | |
| 		}
 | |
| 		return -1;
 | |
| 	}
 | |
| 
 | |
| 	if (create_t1) {
 | |
| 		pthread_join(t1, NULL);
 | |
| 	}
 | |
| 	if (create_t2) {
 | |
| 		pthread_join(t2, NULL);
 | |
| 	}
 | |
| 	pthread_cancel(t3);
 | |
| 
 | |
| 	wl_display_disconnect(display);
 | |
| 	return 0;
 | |
| }
 |