-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclipdaemon.c
108 lines (94 loc) · 3.46 KB
/
clipdaemon.c
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* Copyright (C) 2020-2021 by Ashish Kumar Yadav <[email protected]>
*
* forked from ClipIt, which is
* Copyright (C) 2010-2019 by Cristian Henzel <[email protected]>
* Copyright (C) 2011 by Eugene Nikolsky <[email protected]>
*
* ClipIt is forked from parcellite, which is
* Copyright (C) 2007-2008 by Xyhthyx <[email protected]>
*
* This file is part of clipdaemon.
*
* clipdaemon 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 3 of the License, or
* (at your option) any later version.
*
* clipdaemon is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
#include <glib-object.h>
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include <gobject/gvaluecollector.h>
GtkClipboard *primary;
GtkClipboard *clipboard;
void
primary_handler()
{
static gchar *primary_text;
/* get text in primary selection */
gchar *primary_temp = gtk_clipboard_wait_for_text(primary);
/* check if text in primary selection was lost */
if (!primary_temp && primary_text) {
gint count;
GdkAtom *targets;
/* only recover primary selection if it doesn't have any other type of content */
if (!gtk_clipboard_wait_for_targets(primary, &targets, &count))
gtk_clipboard_set_text(primary, primary_text, -1);
g_free(targets);
} else {
/* get the button state to check if the left mouse button is being held */
GdkModifierType button_state;
GdkDisplay *display = gdk_display_get_default();
GdkScreen *screen = gdk_display_get_default_screen(display);
GdkSeat *seat = gdk_display_get_default_seat(display);
GdkWindow *window = gdk_screen_get_root_window(screen);
GdkDevice *pointer = gdk_seat_get_pointer(seat);
gdk_window_get_device_position_double(window, pointer, NULL, NULL, &button_state);
if (primary_temp && !(button_state & GDK_BUTTON1_MASK)) {
g_free(primary_text);
primary_text = primary_temp;
} else
g_free(primary_temp);
}
}
void
clipboard_handler()
{
static gchar *clipboard_text;
/* get text in clipboard selection */
gchar *clipboard_temp = gtk_clipboard_wait_for_text(clipboard);
/* check if text in clipboard selection was lost */
if (!clipboard_temp && clipboard_text) {
gint count;
GdkAtom *targets;
/* only recover clipboard selection if it doesn't have any other type of content */
if (!gtk_clipboard_wait_for_targets(clipboard, &targets, &count))
gtk_clipboard_set_text(clipboard, clipboard_text, -1);
g_free(targets);
} else {
g_free(clipboard_text);
clipboard_text = clipboard_temp;
}
}
int
main(void)
{
/* initiate GTK+ */
gtk_init(0, NULL);
/* create primary and clipboard selection objects */
primary = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
/* register for clipboard change events */
g_signal_connect(primary, "owner-change", G_CALLBACK(primary_handler), NULL);
g_signal_connect(clipboard, "owner-change", G_CALLBACK(clipboard_handler), NULL);
gtk_main();
return 0;
}