Cosmetics
[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 <X11/Xcursor/Xcursor.h>
23 #include <X11/extensions/XInput2.h>
24 #include <assert.h>
25
26 int main(int argc, char *argv[])
27 {
28         Display* dpy;
29         int screen;
30         Window win;
31
32         int ptrid;
33         char *shape_name;
34         int shape;
35         int size;
36         char *theme;
37         XcursorImage *image;
38         Cursor cursor;
39
40         if (argc < 3 || argc > 4) {
41                 printf("usage: xicursor <ptrid> <shape> [<theme>]\n");
42                 return 1;
43         }
44
45         /* TODO: verify that the pointer id is sane and
46          * that it is a master pointer
47          */
48         ptrid = atoi(argv[1]);
49
50         shape_name = argv[2];
51         shape = XcursorLibraryShape(shape_name);
52         if (shape < 0) {
53                 fprintf(stderr, "Invalid shape name.\n");
54                 return EXIT_FAILURE;
55         }
56
57         dpy = XOpenDisplay(NULL);
58         assert(dpy != NULL);
59
60         /* TODO: check return value? */
61         screen = DefaultScreen(dpy);
62
63         /* TODO: check return value? */
64         win = RootWindow(dpy, screen);
65
66         size = XcursorGetDefaultSize(dpy);
67         if (size == 0) {
68                 fprintf(stderr, "Can't get cursor size.\n");
69                 return EXIT_FAILURE;
70         }
71
72
73         if (argc == 4)
74                 theme = argv[3];
75         else
76                 theme = XcursorGetTheme(dpy);
77                 if (theme == NULL) {
78                         fprintf(stderr, "Can't get cursor theme.\n");
79                         return EXIT_FAILURE;
80                 }
81
82         image = XcursorShapeLoadImage(shape, theme, size);
83         if (image == NULL) {
84                 fprintf(stderr, "Can't get cursor image, check <shape> or <theme>.\n");
85                 return EXIT_FAILURE;
86         }
87
88         cursor = XcursorImageLoadCursor(dpy, image);
89
90         XIDefineCursor(dpy, ptrid, win, cursor);
91         XFlush(dpy);
92
93         XCloseDisplay(dpy);
94         return 0;
95 }