summaryrefslogtreecommitdiff
path: root/gui/clipboard.c
blob: 6f74368a2153fd4de4ae990407fb39fd47f2ac17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
 * gui/clipboard.c - Copy text to clipboard
 *
 * Written 2016 by Werner Almesberger
 * Copyright 2016 by Werner Almesberger
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <stdio.h>

#include "misc/diag.h"
#include "gui/clipboard.h"


#if 0
	/*
	 * This almost works, except that trying to paste on a different screen
	 * produces the following weird behaviour:
	 * - the paste attempt is ignored,
	 * - later, when selecting something else, that something else may get
	 *   pasted.
	 * This behaviour was observed when pasting into an xterm, but may be
	 * more universal.
	 */

	cb = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
	gtk_clipboard_set_text(cb, s, -1);
	cb = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
	gtk_clipboard_set_text(cb, s, -1);

#endif


static void popen_s(const char *cmd, const char *s)
{
	FILE *p;

	p = popen(cmd, "w");
	if (!p) {
		diag_perror(cmd);
		return;
	}
	fprintf(p, "%s", s);
	if (pclose(p) < 0)
		diag_perror(cmd);
}


void copy_to_clipboard(const char *s)
{
	popen_s("xsel -i -p", s);
	popen_s("xsel -i -b", s);
}