Просмотр исходного кода

Fix tsak not destroying old virtual keyboards

Timothy Pearson 5 лет назад
Родитель
Сommit
391a4b2abe
1 измененных файлов: 58 добавлений и 20 удалений
  1. 58
    20
      tsak/main.cpp

+ 58
- 20
tsak/main.cpp Просмотреть файл

@@ -34,6 +34,7 @@ License along with tsak. If not, see http://www.gnu.org/licenses/.
34 34
 #include <sys/stat.h>
35 35
 #include <sys/select.h>
36 36
 #include <sys/time.h>
37
+#include <sys/wait.h>
37 38
 #include <termios.h>
38 39
 #include <signal.h>
39 40
 extern "C" {
@@ -72,6 +73,9 @@ int keyboard_fds[MAX_KEYBOARDS];
72 73
 int child_pids[MAX_KEYBOARDS];
73 74
 int child_led_pids[MAX_KEYBOARDS];
74 75
 
76
+int current_keyboard = -1;
77
+int devout[MAX_KEYBOARDS];
78
+
75 79
 const char *keycode[256] =
76 80
 {
77 81
 	"", "<esc>", "1", "2", "3", "4", "5", "6", "7", "8",
@@ -119,6 +123,15 @@ void signal_callback_handler(int signum)
119 123
 void tsak_friendly_termination() {
120 124
 	int i;
121 125
 
126
+	if ((current_keyboard >= 0) && (devout[current_keyboard] > 0)) {
127
+		if (ioctl(devout[current_keyboard],UI_DEV_DESTROY)<0) {
128
+			fprintf(stderr, "[tsak] Unable to destroy input device with UI_DEV_DESTROY\n");
129
+		}
130
+		else {
131
+			fprintf(stderr, "[tsak] Device destroyed\n");
132
+		}
133
+	}
134
+
122 135
 	// Close down all child processes
123 136
 	for (i=0; i<MAX_KEYBOARDS; i++) {
124 137
 		if (child_pids[i] != 0) {
@@ -378,10 +391,22 @@ void restart_tsak()
378 391
 	// Close down all child processes
379 392
 	for (i=0; i<MAX_KEYBOARDS; i++) {
380 393
 		if (child_pids[i] != 0) {
381
-			kill(child_pids[i], SIGKILL);
394
+			kill(child_pids[i], SIGTERM);
382 395
 		}
383 396
 		if (child_led_pids[i] != 0) {
384
-			kill(child_led_pids[i], SIGKILL);
397
+			kill(child_led_pids[i], SIGTERM);
398
+		}
399
+	}
400
+
401
+	// Wait for child process termination
402
+	for (i=0; i<MAX_KEYBOARDS; i++) {
403
+		if (child_pids[i] != 0) {
404
+			waitpid(child_pids[i], NULL, 0);
405
+			child_pids[i] = 0;
406
+		}
407
+		if (child_led_pids[i] != 0) {
408
+			waitpid(child_led_pids[i], NULL, 0);
409
+			child_led_pids[i] = 0;
385 410
 		}
386 411
 	}
387 412
 
@@ -435,7 +460,6 @@ int main (int argc, char *argv[])
435 460
 	struct input_event event;
436 461
 	struct input_event revev;
437 462
 	struct uinput_user_dev devinfo={{0},{0}};
438
-	int devout[MAX_KEYBOARDS];
439 463
 	int rd;
440 464
 	int i;
441 465
 	int size = sizeof (struct input_event);
@@ -446,7 +470,6 @@ int main (int argc, char *argv[])
446 470
 	bool established = false;
447 471
 	bool testrun = false;
448 472
 	bool depcheck = false;
449
-	int current_keyboard;
450 473
 	bool can_proceed;
451 474
 
452 475
 	// Ignore SIGPIPE
@@ -495,6 +518,19 @@ int main (int argc, char *argv[])
495 518
 			}
496 519
 		}
497 520
 
521
+		if ((testrun == false) && (depcheck == false)) {
522
+			// fork to background
523
+			int i=fork();
524
+			if (i<0) {
525
+				return 10; // fork failed
526
+			}
527
+			if (i>0) {
528
+				// Terminate parent
529
+				controlpipe.active = false;
530
+				return 0;
531
+			}
532
+		}
533
+
498 534
 		while (1) {
499 535
 			if (depcheck == false) {
500 536
 				controlpipe.active = true;
@@ -566,7 +602,7 @@ int main (int argc, char *argv[])
566 602
 					for (current_keyboard=0;current_keyboard<keyboard_fd_num;current_keyboard++) {
567 603
 						if(ioctl(keyboard_fds[current_keyboard], EVIOCGRAB, 2) < 0) {
568 604
 							close(keyboard_fds[current_keyboard]);
569
-							fprintf(stderr, "[tsak] Failed to grab exclusive input device lock");
605
+							fprintf(stderr, "[tsak] Failed to grab exclusive input device lock\n");
570 606
 							if (established) {
571 607
 								sleep(1);
572 608
 							}
@@ -586,10 +622,12 @@ int main (int argc, char *argv[])
586 622
 							}
587 623
 							if (ioctl(devout[current_keyboard],UI_DEV_CREATE)<0) {
588 624
 								fprintf(stderr, "[tsak] Unable to create input device with UI_DEV_CREATE\n");
589
-								if (established)
625
+								if (established) {
590 626
 									sleep(1);
591
-								else
627
+								}
628
+								else {
592 629
 									return 2;
630
+								}
593 631
 							}
594 632
 							else {
595 633
 								fprintf(stderr, "[tsak] Device created.\n");
@@ -636,7 +674,14 @@ int main (int argc, char *argv[])
636 674
 								while (1) {
637 675
 									if ((rd = read(keyboard_fds[current_keyboard], ev, size)) < size) {
638 676
 										fprintf(stderr, "[tsak] Read failed.\n");
639
-										return 13;
677
+										if (ioctl(devout[current_keyboard],UI_DEV_DESTROY)<0) {
678
+											fprintf(stderr, "[tsak] Unable to destroy input device with UI_DEV_DESTROY\n");
679
+											return 13;
680
+										}
681
+										else {
682
+											fprintf(stderr, "[tsak] Device destroyed.\n");
683
+										}
684
+										return 14;
640 685
 									}
641 686
 
642 687
 									if (ev[0].value == 0 && ev[0].type == 1) { // Read the key release event
@@ -677,17 +722,6 @@ int main (int argc, char *argv[])
677 722
 						}
678 723
 					}
679 724
 
680
-					// fork udev monitor process
681
-					int i=fork();
682
-					if (i<0) {
683
-						return 10; // fork failed
684
-					}
685
-					if (i>0) {
686
-						// Terminate parent
687
-						controlpipe.active = false;
688
-						return 0;
689
-					}
690
-
691 725
 					// Close all keyboard file descriptors; we don't need them in this process and they can end up dangling/locked during forced restart
692 726
 					for (int current_keyboard=0;current_keyboard<keyboard_fd_num;current_keyboard++) {
693 727
 						close(keyboard_fds[current_keyboard]);
@@ -695,13 +729,17 @@ int main (int argc, char *argv[])
695 729
 					}
696 730
 					keyboard_fd_num = 0;
697 731
 
732
+					if (testrun == true) {
733
+						return 0;
734
+					}
735
+
698 736
 					// Prevent multiple process instances from starting
699 737
 					setupLockingPipe(true);
700 738
 
701 739
 					// Wait a little bit so that udev hotplug can stabilize before we start monitoring
702 740
 					sleep(1);
703 741
 
704
-					fprintf(stderr, "[tsak] Hotplug monitoring process started (%ld)\n", getpid());
742
+					fprintf(stderr, "[tsak] Hotplug monitoring process started\n");
705 743
 
706 744
 					// Monitor for hotplugged keyboards
707 745
 					int j;

Загрузка…
Отмена
Сохранить