From e723b237f11e18822f7971f1b5d8884b666455eb Mon Sep 17 00:00:00 2001
From: tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>
Date: Sat, 17 Sep 2011 23:27:00 +0000
Subject: Make the SAK only work on the current VT

git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdebase@1254116 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
---
 kdmlib/kdmtsak.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 tsak/main.cpp      |  8 ++++---
 2 files changed, 68 insertions(+), 7 deletions(-)

diff --git a/kdmlib/kdmtsak.cpp b/kdmlib/kdmtsak.cpp
index 967e54855..c1ef5d12f 100644
--- a/kdmlib/kdmtsak.cpp
+++ b/kdmlib/kdmtsak.cpp
@@ -20,8 +20,62 @@
 
 #include "kdmtsak.h"
 
+#include <tqstringlist.h>
+
 #define FIFO_FILE "/tmp/ksocket-global/tsak"
 
+TQString exec(char* cmd) {
+	FILE* pipe = popen(cmd, "r");
+	if (!pipe) return "ERROR";
+	char buffer[128];
+	TQString result = "";
+	while(!feof(pipe)) {
+	if(fgets(buffer, 128, pipe) != NULL)
+		result += buffer;
+	}
+	pclose(pipe);
+	return result;
+}
+
+bool is_vt_active() {
+	const char * currentDisplay;
+	currentDisplay = getenv ("DISPLAY");
+	if (currentDisplay == NULL) {
+		return true;
+	}
+	else {
+		TQString cvtName = "";
+		TQString output = exec("kdmctl list");
+		TQString curConsole = exec("fgconsole");
+		bool intFound;
+		int curConsoleNum = curConsole.toInt(&intFound);
+		if (intFound = false) {
+			return 1;
+		}
+		curConsole = TQString("vt%1").tqarg(curConsoleNum);;
+		TQStringList sessionList = TQStringList::split('\t', output, false);
+		for ( TQStringList::Iterator it = sessionList.begin(); it != sessionList.end(); ++it ) {
+			TQStringList sessionInfoList = TQStringList::split(',', *it, true);
+			if ((*(sessionInfoList.at(0))).startsWith(":")) {
+				if ((*(sessionInfoList.at(1))) == TQString(curConsole)) {
+					cvtName = (*(sessionInfoList.at(0)));
+				}
+			}
+		}
+		if (cvtName != "") {
+			if (TQString(currentDisplay).startsWith(cvtName)) {
+				return true;
+			}
+			else {
+				return false;
+			}
+		}
+		else {
+			return true;
+		}
+	}
+}
+
 int main (int argc, char *argv[])
 {
 	int mPipe_fd;
@@ -33,20 +87,25 @@ int main (int argc, char *argv[])
 	if (verifier_result == 0) {
 			// OK, the calling process is authorized to retrieve SAK data
 			// First, flush the buffer
-			mPipe_fd = open(FIFO_FILE, O_RDONLY | O_NONBLOCK);
+			mPipe_fd = open(FIFO_FILE, O_RDWR | O_NONBLOCK);
 			numread = 1;
 			while (numread > 0) {
 				numread = read(mPipe_fd, readbuf, 6);
 			}
 			// Now wait for SAK press
-			mPipe_fd = open(FIFO_FILE, O_RDONLY);
+			mPipe_fd = open(FIFO_FILE, O_RDWR);
 			while (mPipe_fd > -1) {
 				numread = read(mPipe_fd, readbuf, 6);
 				readbuf[numread] = 0;
 				readbuf[127] = 0;
 				if (strcmp(readbuf, "SAK\n\r") == 0) {
-					close(mPipe_fd);
-					return 0;
+					if (is_vt_active()) {
+						close(mPipe_fd);
+						return 0;
+					}
+					else {
+						usleep(100);
+					}
 				}
 				else {
 					usleep(100);
diff --git a/tsak/main.cpp b/tsak/main.cpp
index 57e228c2e..2b51dbc85 100644
--- a/tsak/main.cpp
+++ b/tsak/main.cpp
@@ -329,9 +329,11 @@ int main (int argc, char *argv[])
 							}
 							if (hide_event == true) {
 								// Let anyone listening to our interface know that an SAK keypress was received
-								write(mPipe_fd_out, "SAK\n\r", 6);
-								write(mPipe_fd_out, "SAK\n\r", 6);
-								write(mPipe_fd_out, "SAK\n\r", 6);
+								// I highly doubt there are more than 255 VTs active at once...
+								int i;
+								for (i=0;i<255;i++) {
+									write(mPipe_fd_out, "SAK\n\r", 6);
+								}
 							}
 						}
 					}
-- 
cgit v1.2.3

