Skip to content

Instantly share code, notes, and snippets.

@Daniel-Abrecht
Created April 11, 2023 21:17
Show Gist options
  • Save Daniel-Abrecht/6a0ec0f0170bd63ce0d5076a66d6d9b1 to your computer and use it in GitHub Desktop.
Save Daniel-Abrecht/6a0ec0f0170bd63ce0d5076a66d6d9b1 to your computer and use it in GitHub Desktop.
Periodic tty locker
#include <libttymultiplex.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
const static struct tym_super_position_rectangle fullscreen_coordinates = {
.edge[TYM_RECT_TOP_LEFT].type[TYM_P_CHARFIELD].axis[TYM_AXIS_VERTICAL].value.integer = 1,
.edge[TYM_RECT_BOTTOM_RIGHT].type[TYM_P_RATIO].axis = {
[TYM_AXIS_HORIZONTAL].value.real = 1,
[TYM_AXIS_VERTICAL].value.real = 1,
},
};
const static struct tym_super_position_rectangle message_coordinates = {
.edge[TYM_RECT_BOTTOM_RIGHT].type[TYM_P_RATIO].axis[TYM_AXIS_HORIZONTAL].value.real = 1,
.edge[TYM_RECT_BOTTOM_RIGHT].type[TYM_P_CHARFIELD].axis[TYM_AXIS_VERTICAL].value.integer = 1,
};
bool exec_in_pane(int pane, char*const argv[]){
if(tym_freeze() == -1){
fprintf(stderr, "tym_freeze failed");
return 1;
}
pid_t newpid = fork();
if(newpid == -1){
tym_init();
return false;
}
if(newpid){
tym_init();
return true;
}
if(tym_pane_set_env(pane) == -1)
return -1;
tym_zap();
execvp(argv[0], argv);
}
int main(){
tym_init();
int main_pane = tym_pane_create(&fullscreen_coordinates);
int message_pane = tym_pane_create(&message_coordinates);
int message_fd = tym_pane_get_slavefd(message_pane);
//tym_pane_set_flag(message_pane, TYM_PF_DISALLOW_FOCUS, true);
if(main_pane==-1 || message_pane==-1){
fprintf(stderr, "tym_pane_create failed\n");
return 1;
}
if(!exec_in_pane(main_pane, (char*const[]){"bash",0})){
fprintf(stderr, "exec_in_pane failed\n");
return 1;
}
bool locked = false;
int time = 0;
tym_pane_set_flag(main_pane, TYM_PF_FOCUS, true);
while(1){
int ret = waitpid(-1,0,WNOHANG);
if(ret == -1){
if(errno == EINTR)
continue;
if(errno != EAGAIN)
break;
}
sleep(1);
int d = locked ? 5 : 10;
int remaining = d - time;
time += 1;
if(!remaining){
locked = !locked;
if(locked){
// Not working, bug
//tym_pane_set_flag(main_pane, TYM_PF_FOCUS, false);
tym_pane_set_flag(main_pane, TYM_PF_DISALLOW_FOCUS, true);
// workaround
tym_pane_set_flag(message_pane, TYM_PF_FOCUS, true);
}else{
tym_pane_set_flag(main_pane, TYM_PF_DISALLOW_FOCUS, false);
tym_pane_set_flag(main_pane, TYM_PF_FOCUS, true);
}
time = 0;
}
dprintf(message_fd, "\r\x1b[2JPane will %s in: %d", locked ? "unlock" : "lock", remaining);
}
tym_shutdown();
return 0;
}
# https://github.com/Daniel-Abrecht/libttymultiplex
LDLIBS=-lttymultiplex
all: main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment