commit f195793

Michael Forney  ·  2018-11-14 08:17:41 +0000 UTC
parent 9bb3730
launch: Style
1 files changed,  +37, -51
+37, -51
  1@@ -59,10 +59,8 @@ pid_t child_pid;
  2 
  3 static struct {
  4 	int socket;
  5-	int input_fds[128];
  6-	unsigned num_input_fds;
  7-	int drm_fds[16];
  8-	unsigned num_drm_fds;
  9+	int input_fds[128], num_input_fds;
 10+	int drm_fds[16], num_drm_fds;
 11 	int tty_fd;
 12 	bool active;
 13 } launcher;
 14@@ -77,8 +75,7 @@ static struct {
 15 static bool nflag;
 16 static int sigfd[2];
 17 
 18-static noreturn void __attribute__((format(printf, 1, 2)))
 19-die(const char *format, ...);
 20+static void cleanup(void);
 21 
 22 static noreturn void usage(const char *name)
 23 {
 24@@ -86,13 +83,30 @@ static noreturn void usage(const char *name)
 25 	exit(2);
 26 }
 27 
 28+static noreturn void __attribute__((format(printf, 1, 2)))
 29+die(const char *format, ...)
 30+{
 31+	va_list args;
 32+
 33+	va_start(args, format);
 34+	vfprintf(stderr, format, args);
 35+	va_end(args);
 36+
 37+	if (format[0] && format[strlen(format) - 1] == ':')
 38+		fprintf(stderr, " %s", strerror(errno));
 39+	fputc('\n', stderr);
 40+
 41+	cleanup();
 42+	exit(EXIT_FAILURE);
 43+}
 44+
 45 static void
 46 start_devices(void)
 47 {
 48-	unsigned index;
 49+	int i;
 50 
 51-	for (index = 0; index < launcher.num_drm_fds; ++index) {
 52-		if (drmSetMaster(launcher.drm_fds[index]) < 0)
 53+	for (i = 0; i < launcher.num_drm_fds; ++i) {
 54+		if (drmSetMaster(launcher.drm_fds[i]) < 0)
 55 			die("failed to set DRM master");
 56 	}
 57 }
 58@@ -100,26 +114,24 @@ start_devices(void)
 59 static void
 60 stop_devices(bool fatal)
 61 {
 62-	unsigned index;
 63+	int i;
 64 
 65-	for (index = 0; index < launcher.num_drm_fds; ++index) {
 66-		if (drmDropMaster(launcher.drm_fds[index]) < 0 && fatal)
 67-			die("failed to drop DRM master");
 68+	for (i = 0; i < launcher.num_drm_fds; ++i) {
 69+		if (drmDropMaster(launcher.drm_fds[i]) < 0 && fatal)
 70+			die("drmDropMaster:");
 71 	}
 72-
 73-	for (index = 0; index < launcher.num_input_fds; ++index) {
 74-		if (ioctl(launcher.input_fds[index], EVIOCREVOKE, 0) < 0 && errno != ENODEV && fatal)
 75-			die("failed to revoke input device:");
 76-		close(launcher.input_fds[index]);
 77+	for (i = 0; i < launcher.num_input_fds; ++i) {
 78+		if (ioctl(launcher.input_fds[i], EVIOCREVOKE, 0) < 0 && errno != ENODEV && fatal)
 79+			die("ioctl EVIOCREVOKE:");
 80+		close(launcher.input_fds[i]);
 81 	}
 82-
 83 	launcher.num_input_fds = 0;
 84 }
 85 
 86 static void
 87 cleanup(void)
 88 {
 89-	struct vt_mode mode = {.mode = VT_AUTO };
 90+	struct vt_mode mode = {.mode = VT_AUTO};
 91 
 92 	if (!original_vt_state.altered)
 93 		return;
 94@@ -138,27 +150,10 @@ cleanup(void)
 95 		kill(child_pid, SIGTERM);
 96 }
 97 
 98-noreturn void __attribute__((format(printf, 1, 2)))
 99-die(const char *format, ...)
100-{
101-	va_list args;
102-
103-	va_start(args, format);
104-	vfprintf(stderr, format, args);
105-	va_end(args);
106-
107-	if (format[0] && format[strlen(format) - 1] == ':')
108-		fprintf(stderr, " %s", strerror(errno));
109-	fputc('\n', stderr);
110-
111-	cleanup();
112-	exit(EXIT_FAILURE);
113-}
114-
115 static void
116 activate(void)
117 {
118-	struct swc_launch_event event = {.type = SWC_LAUNCH_EVENT_ACTIVATE };
119+	struct swc_launch_event event = {.type = SWC_LAUNCH_EVENT_ACTIVATE};
120 
121 	start_devices();
122 	send(launcher.socket, &event, sizeof(event), 0);
123@@ -168,7 +163,7 @@ activate(void)
124 static void
125 deactivate(void)
126 {
127-	struct swc_launch_event event = {.type = SWC_LAUNCH_EVENT_DEACTIVATE };
128+	struct swc_launch_event event = {.type = SWC_LAUNCH_EVENT_DEACTIVATE};
129 
130 	send(launcher.socket, &event, sizeof(event), 0);
131 	stop_devices(true);
132@@ -215,7 +210,6 @@ handle_socket_data(int socket)
133 		case INPUT_MAJOR:
134 			if (!launcher.active)
135 				goto fail;
136-
137 			if (launcher.num_input_fds == ARRAY_LENGTH(launcher.input_fds)) {
138 				fprintf(stderr, "too many input devices opened\n");
139 				goto fail;
140@@ -275,12 +269,11 @@ static void
141 find_vt(char *vt, size_t size)
142 {
143 	char *vtnr;
144+	int tty0_fd, vt_num;
145 
146 	/* If we are running from an existing X or wayland session, always open a new
147 	 * VT instead of using the current one. */
148 	if (getenv("DISPLAY") || getenv("WAYLAND_DISPLAY") || !(vtnr = getenv("XDG_VTNR"))) {
149-		int tty0_fd, vt_num;
150-
151 		tty0_fd = open("/dev/tty0", O_RDWR);
152 		if (tty0_fd == -1)
153 			die("open /dev/tty0:");
154@@ -326,39 +319,32 @@ setup_tty(int fd)
155 
156 	if (fstat(fd, &st) == -1)
157 		die("failed to stat TTY fd:");
158-
159 	vt = minor(st.st_rdev);
160-
161 	if (major(st.st_rdev) != TTY_MAJOR || vt == 0)
162 		die("not a valid VT");
163 
164 	if (ioctl(fd, VT_GETSTATE, &state) == -1)
165 		die("failed to get the current VT state:");
166-
167 	original_vt_state.vt = state.v_active;
168-
169 	if (ioctl(fd, KDGKBMODE, &original_vt_state.kb_mode))
170 		die("failed to get keyboard mode:");
171-
172 	if (ioctl(fd, KDGETMODE, &original_vt_state.console_mode))
173 		die("failed to get console mode:");
174 
175 	if (ioctl(fd, KDSKBMODE, K_OFF) == -1)
176 		die("failed to set keyboard mode to K_OFF:");
177-
178 	if (ioctl(fd, KDSETMODE, KD_GRAPHICS) == -1) {
179 		perror("failed to set console mode to KD_GRAPHICS");
180 		goto error0;
181 	}
182-
183 	if (ioctl(fd, VT_SETMODE, &mode) == -1) {
184 		perror("failed to set VT mode");
185 		goto error1;
186 	}
187 
188-	if (vt == original_vt_state.vt)
189+	if (vt == original_vt_state.vt) {
190 		activate();
191-	else if (!nflag) {
192+	} else if (!nflag) {
193 		if (ioctl(fd, VT_ACTIVATE, vt) == -1) {
194 			perror("failed to activate VT");
195 			goto error2;