Skip to content

Commit

Permalink
Clear global clipboard after specified seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
alimirjamali committed Nov 14, 2024
1 parent 89d0254 commit 0b77a64
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
46 changes: 46 additions & 0 deletions gui-daemon/xside.c
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,7 @@ static void save_clipboard_metadata(struct clipboard_metadata *metadata) {
fprintf(file, "\"buffer_size\":%d,\n", metadata->buffer_size);
fprintf(file, "\"protocol_version_xside\":%d,\n", metadata->protocol_version_xside);
fprintf(file, "\"protocol_version_vmside\":%d\n", metadata->protocol_version_vmside);
fprintf(file, "\"timeout\":%d\n", metadata->timeout);
// other key,value pairs could be added if needed in future
fprintf(file, "}\n");
fclose(file);
Expand Down Expand Up @@ -932,6 +933,8 @@ static bool load_clipboard_metadata(struct clipboard_metadata *metadata, bool lo
metadata->protocol_version_vmside = dummy;
} else if (strcmp(key, "protocol_version_xside") == 0) {
metadata->protocol_version_xside = dummy;
} else if (strcmp(key, "timeout") == 0) {
metadata->timeout = dummy;
}
}
fclose(file);
Expand Down Expand Up @@ -1192,6 +1195,7 @@ static void handle_clipboard_data(Ghandles * g, unsigned int untrusted_len)
metadata.protocol_version_vmside = g->protocol_version;
metadata.protocol_version_xside = PROTOCOL_VERSION(
PROTOCOL_VERSION_MAJOR, PROTOCOL_VERSION_MINOR);
metadata.timeout = g->clipboard_timeout;

if (g->log_level > 0)
fprintf(stderr, "handle_clipboard_data, len=0x%x\n",
Expand Down Expand Up @@ -1274,6 +1278,42 @@ static void handle_clipboard_data(Ghandles * g, unsigned int untrusted_len)
save_clipboard_file_xevent_timestamp(g->clipboard_xevent_time);
metadata.successful = true;
save_clipboard_metadata(&metadata);

if (g->clipboard_timeout > 0) {
struct stat st0, st1;
if (lstat(QUBES_CLIPBOARD_FILENAME, &st0) == -1) {
perror("Can not get clipboard data file status");
exit(1);
}
switch (fork()) {
case -1:
perror("fork");
exit(1);
case 0:
if (sleep(g->clipboard_timeout) != 0) {
perror("Clipboard clearing timeout was interrupted");
_exit(1);
}
inter_appviewer_lock(g, 1);
if (lstat(QUBES_CLIPBOARD_FILENAME, &st1) == -1) {
perror("Can not get clipboard data file status");
_exit(1);
}
if (st0.st_mtime == st1.st_mtime) {
old_umask = umask(0007);
metadata.copy_action = false;
metadata.timeout = g->clipboard_timeout;
clear_clipboard(&metadata);
umask(old_umask);
if (g->log_level > 1)
fprintf(stderr, "Clipboard cleared after %d seconds\n",
g->clipboard_timeout);
}
inter_appviewer_lock(g, 0);
_exit(1);
}
}

error:
umask(old_umask);
inter_appviewer_lock(g, 0);
Expand Down Expand Up @@ -4346,6 +4386,7 @@ static void load_default_config_values(Ghandles * g)
g->paste_seq_mask = ControlMask | ShiftMask;
g->paste_seq_key = XK_v;
g->clipboard_buffer_size = DEFAULT_CLIPBOARD_BUFFER_SIZE;
g->clipboard_timeout = 0;
g->allow_fullscreen = 0;
g->override_redirect_protection = 1;
g->startup_timeout = 45;
Expand Down Expand Up @@ -4430,6 +4471,11 @@ static void parse_vm_config(Ghandles * g, config_setting_t * group)
}
}

if ((setting =
config_setting_get_member(group, "clipboard_timeout"))) {
g->clipboard_timeout = config_setting_get_int(setting);
}

if ((setting =
config_setting_get_member(group, "allow_utf8_titles"))) {
g->allow_utf8_titles = config_setting_get_bool(setting);
Expand Down
2 changes: 2 additions & 0 deletions gui-daemon/xside.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ struct _global_handles {
KeySym paste_seq_key; /* key for secure-paste key sequence */
unsigned int clipboard_buffer_size; /* maximum clipboard size limit */
int qrexec_clipboard; /* 0: use GUI protocol to fetch/put clipboard, 1: use qrexec */
unsigned int clipboard_timeout; /* x: clear global clipboard after x seconds, 0: do not clear */
int use_kdialog; /* use kdialog for prompts (default on KDE) or zenity (default on non-KDE) */
int prefix_titles; /* prefix windows titles with VM name (for WM without support for _QUBES_VMNAME property) */
enum trayicon_mode trayicon_mode; /* trayicon coloring mode */
Expand Down Expand Up @@ -322,6 +323,7 @@ struct clipboard_metadata {
unsigned int buffer_size; /* maximum allowed clipboard size for this vm */
uint32_t protocol_version_vmside; /* inter-vm clipboard GUI protocol version - vmside */
uint32_t protocol_version_xside; /* inter-vm clipboard GUI protocol version - xside */
unsigned int timeout; /* 0: Clipboard is/will not automatically cleared. x: is/will be cleared after x seconds */
};

#define QUBES_NO_SHM_SEGMENT ((xcb_shm_seg_t)-1)
Expand Down

0 comments on commit 0b77a64

Please sign in to comment.