-
Notifications
You must be signed in to change notification settings - Fork 6
/
swcursor.c
104 lines (79 loc) · 2.45 KB
/
swcursor.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
#include <stdlib.h>
#include <stdio.h>
#include <X11/Xlib.h>
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include "swcursor-window.h"
static cairo_surface_t *load_image(const char *path);
static void show_main_window(cairo_surface_t *image;);
static gboolean tick(GtkWidget *widget, GdkFrameClock *frame_clock, gpointer user_data);
int main(int argc, char **argv)
{
gtk_init (&argc, &argv);
cairo_surface_t *image;
if (argc >= 2) {
image = load_image(argv[1]);
} else {
image = load_image("cursor.png");
}
show_main_window(image);
gtk_main();
return 0;
}
static cairo_surface_t *load_image(const char *path)
{
cairo_surface_t *image = cairo_image_surface_create_from_png(path);
if (cairo_surface_status(image) != CAIRO_STATUS_SUCCESS) {
const char *msg = cairo_status_to_string(cairo_surface_status(image));
fprintf(stderr, "swcursor: error loading '%s': %s\n", path, msg);
exit(1);
}
return image;
}
static void show_main_window(cairo_surface_t *image)
{
SWCursorWindow *window;
window = swcursor_window_new();
swcursor_window_set_image(window, image);
gtk_widget_add_tick_callback(GTK_WIDGET (window), tick, NULL, NULL);
gtk_widget_show_all(GTK_WIDGET (window));
}
static gboolean tick(GtkWidget *widget, GdkFrameClock *frame_clock, gpointer user_data)
{
static gboolean show_warning = TRUE;
GdkWindow *gdk_window;
Display *xdisplay;
Window xroot_window;
Window ret_root;
Window ret_child;
int root_x, root_y;
int win_x, win_y;
int move_x, move_y;
unsigned int mask;
gint scale_factor;
gboolean mouse_down;
gdk_window = gtk_widget_get_window(widget);
xdisplay = GDK_SCREEN_XDISPLAY(gdk_window_get_screen(gdk_window));
xroot_window = XDefaultRootWindow(xdisplay);
scale_factor = gdk_window_get_scale_factor(gdk_window);
if(XQueryPointer(xdisplay, xroot_window,
&ret_root, &ret_child,
&root_x, &root_y,
&win_x, &win_y, &mask))
{
move_x = root_x / scale_factor - 1;
move_y = root_y / scale_factor - 1;
gtk_window_move(GTK_WINDOW (widget), move_x, move_y);
mouse_down = (mask & Button1Mask) ||
(mask & Button2Mask) ||
(mask & Button3Mask);
swcursor_window_set_mouse_down(SWCURSOR_WINDOW (widget), mouse_down);
} else {
if (show_warning) {
fprintf(stderr, "swcursor: warning: could not get cursor position from Xserver (further warnings will be suppressed)");
show_warning = FALSE;
}
}
return G_SOURCE_CONTINUE;
}