cursor.c revision 6d8e82c3
1/*
2 *
3Copyright 1989, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24 * */
25
26/***********************************************************************
27 *
28 * cursor creation code
29 *
30 * 05-Apr-89 Thomas E. LaStrange	File created
31 *
32 ***********************************************************************/
33
34#include <stdio.h>
35#include "twm.h"
36#include <X11/Xos.h>
37#include "screen.h"
38#include "util.h"
39
40/* *INDENT-OFF* */
41static struct _CursorName {
42    const char		*name;
43    unsigned int	shape;
44    Cursor		cursor;
45} cursor_names[] = {
46
47{"X_cursor",		XC_X_cursor,		None},
48{"arrow",		XC_arrow,		None},
49{"based_arrow_down",	XC_based_arrow_down,    None},
50{"based_arrow_up",	XC_based_arrow_up,      None},
51{"boat",		XC_boat,		None},
52{"bogosity",		XC_bogosity,		None},
53{"bottom_left_corner",	XC_bottom_left_corner,  None},
54{"bottom_right_corner",	XC_bottom_right_corner, None},
55{"bottom_side",		XC_bottom_side,		None},
56{"bottom_tee",		XC_bottom_tee,		None},
57{"box_spiral",		XC_box_spiral,		None},
58{"center_ptr",		XC_center_ptr,		None},
59{"circle",		XC_circle,		None},
60{"clock",		XC_clock,		None},
61{"coffee_mug",		XC_coffee_mug,		None},
62{"cross",		XC_cross,		None},
63{"cross_reverse",	XC_cross_reverse,       None},
64{"crosshair",		XC_crosshair,		None},
65{"diamond_cross",	XC_diamond_cross,       None},
66{"dot",			XC_dot,			None},
67{"dotbox",		XC_dotbox,		None},
68{"double_arrow",	XC_double_arrow,	None},
69{"draft_large",		XC_draft_large,		None},
70{"draft_small",		XC_draft_small,		None},
71{"draped_box",		XC_draped_box,		None},
72{"exchange",		XC_exchange,		None},
73{"fleur",		XC_fleur,		None},
74{"gobbler",		XC_gobbler,		None},
75{"gumby",		XC_gumby,		None},
76{"hand1",		XC_hand1,		None},
77{"hand2",		XC_hand2,		None},
78{"heart",		XC_heart,		None},
79{"icon",		XC_icon,		None},
80{"iron_cross",		XC_iron_cross,		None},
81{"left_ptr",		XC_left_ptr,		None},
82{"left_side",		XC_left_side,		None},
83{"left_tee",		XC_left_tee,		None},
84{"leftbutton",		XC_leftbutton,		None},
85{"ll_angle",		XC_ll_angle,		None},
86{"lr_angle",		XC_lr_angle,		None},
87{"man",			XC_man,			None},
88{"middlebutton",	XC_middlebutton,	None},
89{"mouse",		XC_mouse,		None},
90{"pencil",		XC_pencil,		None},
91{"pirate",		XC_pirate,		None},
92{"plus",		XC_plus,		None},
93{"question_arrow",	XC_question_arrow,	None},
94{"right_ptr",		XC_right_ptr,		None},
95{"right_side",		XC_right_side,		None},
96{"right_tee",		XC_right_tee,		None},
97{"rightbutton",		XC_rightbutton,		None},
98{"rtl_logo",		XC_rtl_logo,		None},
99{"sailboat",		XC_sailboat,		None},
100{"sb_down_arrow",	XC_sb_down_arrow,       None},
101{"sb_h_double_arrow",	XC_sb_h_double_arrow,   None},
102{"sb_left_arrow",	XC_sb_left_arrow,       None},
103{"sb_right_arrow",	XC_sb_right_arrow,      None},
104{"sb_up_arrow",		XC_sb_up_arrow,		None},
105{"sb_v_double_arrow",	XC_sb_v_double_arrow,   None},
106{"shuttle",		XC_shuttle,		None},
107{"sizing",		XC_sizing,		None},
108{"spider",		XC_spider,		None},
109{"spraycan",		XC_spraycan,		None},
110{"star",		XC_star,		None},
111{"target",		XC_target,		None},
112{"tcross",		XC_tcross,		None},
113{"top_left_arrow",	XC_top_left_arrow,      None},
114{"top_left_corner",	XC_top_left_corner,	None},
115{"top_right_corner",	XC_top_right_corner,    None},
116{"top_side",		XC_top_side,		None},
117{"top_tee",		XC_top_tee,		None},
118{"trek",		XC_trek,		None},
119{"ul_angle",		XC_ul_angle,		None},
120{"umbrella",		XC_umbrella,		None},
121{"ur_angle",		XC_ur_angle,		None},
122{"watch",		XC_watch,		None},
123{"xterm",		XC_xterm,		None},
124};
125/* *INDENT-ON* */
126
127void
128NewFontCursor(Cursor *cp, const char *str)
129{
130    int i;
131
132    for (i = 0; (size_t) i < sizeof(cursor_names) / sizeof(struct _CursorName);
133         i++) {
134        if (strcmp(str, cursor_names[i].name) == 0) {
135            if (cursor_names[i].cursor == None)
136                cursor_names[i].cursor =
137                    XCreateFontCursor(dpy, cursor_names[i].shape);
138            *cp = cursor_names[i].cursor;
139            return;
140        }
141    }
142    twmWarning("unable to find font cursor \"%s\"", str);
143}
144
145void
146NewBitmapCursor(Cursor *cp, char *source, char *mask)
147{
148    int hotx, hoty;
149    int sx, sy, mx, my;
150    unsigned int sw, sh, mw, mh;
151    Pixmap spm, mpm;
152    unsigned udummy = 0;
153    Window wdummy = None;
154
155    spm = GetBitmap(source);
156    if ((hotx = HotX) < 0)
157        hotx = 0;
158    if ((hoty = HotY) < 0)
159        hoty = 0;
160    mpm = GetBitmap(mask);
161
162    /* make sure they are the same size */
163
164    XGetGeometry(dpy, spm, &wdummy, &sx, &sy, &sw, &sh, &udummy, &udummy);
165    XGetGeometry(dpy, mpm, &wdummy, &mx, &my, &mw, &mh, &udummy, &udummy);
166    if (sw != mw || sh != mh) {
167        twmWarning("cursor bitmaps \"%s\" and \"%s\" not the same size\n",
168                   source, mask);
169        return;
170    }
171    *cp = XCreatePixmapCursor(dpy, spm, mpm, &Scr->PointerForeground,
172                              &Scr->PointerBackground,
173                              (unsigned) hotx, (unsigned) hoty);
174}
175