README: mention also pkg-config as a build dependency
[xicursorset.git] / xicursorset.c
1 /* 
2  * xicursorset - set cursor image for an XInput2 pointer
3  *
4  * Copyright (C) 2010 Antonio Ospite <ospite@studenti.unina.it>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <libgen.h>
23 #include <X11/Xcursor/Xcursor.h>
24 #include <X11/extensions/XInput2.h>
25 #include <assert.h>
26
27 int main(int argc, char *argv[])
28 {
29         Display* dpy;
30         int screen;
31         Window win;
32
33         int ptrid;
34         char *shape_name;
35         int shape;
36         int size;
37         char *theme;
38         XcursorImage *image;
39         Cursor cursor;
40
41         if (argc < 3 || argc > 4) {
42                 printf("usage: %s <ptrid> <shape> [<theme>]\n",
43                                 basename(argv[0]));
44                 return 1;
45         }
46
47         /* TODO: verify that the pointer id is sane and
48          * that it is a master pointer
49          */
50         ptrid = atoi(argv[1]);
51
52         shape_name = argv[2];
53         shape = XcursorLibraryShape(shape_name);
54         if (shape < 0) {
55                 fprintf(stderr, "Invalid shape name.\n");
56                 return EXIT_FAILURE;
57         }
58
59         dpy = XOpenDisplay(NULL);
60         assert(dpy != NULL);
61
62         /* TODO: check return value? */
63         screen = DefaultScreen(dpy);
64
65         /* TODO: check return value? */
66         win = RootWindow(dpy, screen);
67
68         size = XcursorGetDefaultSize(dpy);
69         if (size == 0) {
70                 fprintf(stderr, "Can't get cursor size.\n");
71                 return EXIT_FAILURE;
72         }
73
74         if (argc == 4) {
75                 theme = argv[3];
76         } else {
77                 theme = XcursorGetTheme(dpy);
78                 if (theme == NULL) {
79                         fprintf(stderr, "Can't get cursor theme.\n");
80                         return EXIT_FAILURE;
81                 }
82         }
83
84         image = XcursorShapeLoadImage((unsigned int)shape, theme, size);
85         if (image == NULL) {
86                 fprintf(stderr, "Can't get cursor image, check <shape> or <theme>.\n");
87                 return EXIT_FAILURE;
88         }
89
90         cursor = XcursorImageLoadCursor(dpy, image);
91
92         XIDefineCursor(dpy, ptrid, win, cursor);
93         XFlush(dpy);
94
95         XCloseDisplay(dpy);
96         return 0;
97 }