The code below creates two panes in tmux and counts down to 20.
#include <iostream>
#include <fstream>
#include <thread>
#include <vector>
#include <chrono>
#include <atomic>
#include <mutex>
#include <string>
#include <array>
#include <memory>
// Function to execute a shell command and get the result
std::string exec(const std::string& cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
// Function to be executed by each thread
void threadFunction(const std::string& tty, std::atomic<bool>& running, int threadId) {
int messageCount = 0;
std::ofstream ttyStream(tty);
if (!ttyStream.is_open()) {
std::cerr << "Failed to open tty: " << tty << std::endl;
return;
}
while (running.load()) {
ttyStream << "Thread " << threadId << " is running: message " << messageCount++ << std::endl;
ttyStream.flush();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
void kill(const std::string& sessionName){
std::string command = "tmux send-keys -t " + sessionName + " " tmux kill-server" Enter";
// Capture output for debugging
std::string output = exec(command); // Assuming `exec` returns the output
std::cerr << "Command output: " << output << std::endl;
if (std::system(command.c_str()) != 0) {
std::cerr << "Error executing command: " << command << std::endl;
}
}
int main() {
const std::string sessionName = "Client";
const int numThreads = 2; // Number of threads and panes
// Create tmux session and panes, and get their TTYs
std::string createSessionCmd = "tmux new-session -d -s " + sessionName + " -PF '#{pane_id}' 'tail -f /dev/null'";
std::string pane1 = exec(createSessionCmd);
pane1.pop_back(); // Remove newline character
std::string splitPaneCmd = "tmux split-window -t " + sessionName + " -PF '#{pane_id}' 'tail -f /dev/null'";
std::string pane2 = exec(splitPaneCmd);
pane2.pop_back(); // Remove newline character
std::string getTtyCmd1 = "tmux display-message -p -t " + pane1 + " '#{pane_tty}'";
std::string tty1 = exec(getTtyCmd1);
tty1.pop_back(); // Remove newline character
std::string getTtyCmd2 = "tmux display-message -p -t " + pane2 + " '#{pane_tty}'";
std::string tty2 = exec(getTtyCmd2);
tty2.pop_back(); // Remove newline character
std::cout << "TTY1: " << tty1 << std::endl;
std::cout << "TTY2: " << tty2 << std::endl;
std::atomic<bool> running(true);
// Create and run threads
std::thread t1(threadFunction, tty1, std::ref(running), 1);
std::thread t2(threadFunction, tty2, std::ref(running), 2);
std::thread t3([&sessionName]() {
std::this_thread::sleep_for(std::chrono::seconds(20)); // Increase sleep time for testing
std::string command = "tmux new-window -t " + sessionName + " -n thread" + std::to_string(2);
std::system(command.c_str());
kill(sessionName);
});
t1.detach();
t2.detach();
t3.detach();
// Attach to the tmux session
std::string cmd = "tmux attach -t " + sessionName;
std::system(cmd.c_str());
return 0;
}
The problem I am facing is that the panes are not able to take any input. I want thread to display messages on the assigned pane. But if a user wants to send message the pane can take any input then this input should be displayed in the other pane. Meaning the counting messages stop when it is taking input and then display the message and start counting from where it left off. But in the above code it can’t do that but not just that but I can’t run any command. So even to close the tmux session I have to create a new tmux window and use kill-server on the new tmux window.
If I don’t use tty then I have to make it so that in each pane whenever an user enters a string of characters to not consider it as a command and write those into a file and then will need to have a new thread to read from this file and display that in particular pane.
But when I don’t use tty then each “input + Enter” gives the output that input was not a command and as you can see on the top pane to display anything the cpp file needs to run “echo ‘message’ + Enter”. Meaning the message displays twice