1 /* -*-c-*- */
2 /* This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, see: <http://www.gnu.org/licenses/>
14  */
15 
16 /*  Modification History */
17 
18 /*  Created on 10/05/01 by Dan Espen (dane):
19     Extracted from fvwm/cursor.c.
20     Contains common routine to verify and convert a cursor name
21     into a cursor number from X11/cursorfont.h.
22     Used by fvwm CursorStyle command and FvwmForm.
23 */
24 #include "config.h"
25 #include <stdio.h>
26 #include "fvwmlib.h"
27 #include <X11/cursorfont.h>
28 #include "Cursor.h"
29 
30 /*
31  *  fvwmCursorNameToIndex: return the number of a X11 cursor from its
32  *  name, if not found return -1.
33  */
fvwmCursorNameToIndex(char * cursor_name)34 int fvwmCursorNameToIndex (char *cursor_name)
35 {
36 	static const struct CursorNameIndex {
37 		const char  *name;
38 		unsigned int number;
39 	} cursor_table[] = {
40 		{"arrow", XC_arrow},
41 		{"based_arrow_down", XC_based_arrow_down},
42 		{"based_arrow_up", XC_based_arrow_up},
43 		{"boat", XC_boat},
44 		{"bogosity", XC_bogosity},
45 		{"bottom_left_corner", XC_bottom_left_corner},
46 		{"bottom_right_corner", XC_bottom_right_corner},
47 		{"bottom_side", XC_bottom_side},
48 		{"bottom_tee", XC_bottom_tee},
49 		{"box_spiral", XC_box_spiral},
50 		{"center_ptr", XC_center_ptr},
51 		{"circle", XC_circle},
52 		{"clock", XC_clock},
53 		{"coffee_mug", XC_coffee_mug},
54 		{"cross", XC_cross},
55 		{"cross_reverse", XC_cross_reverse},
56 		{"crosshair", XC_crosshair},
57 		{"diamond_cross", XC_diamond_cross},
58 		{"dot", XC_dot},
59 		{"dotbox", XC_dotbox},
60 		{"double_arrow", XC_double_arrow},
61 		{"draft_large", XC_draft_large},
62 		{"draft_small", XC_draft_small},
63 		{"draped_box", XC_draped_box},
64 		{"exchange", XC_exchange},
65 		{"fleur", XC_fleur},
66 		{"gobbler", XC_gobbler},
67 		{"gumby", XC_gumby},
68 		{"hand1", XC_hand1},
69 		{"hand2", XC_hand2},
70 		{"heart", XC_heart},
71 		{"icon", XC_icon},
72 		{"iron_cross", XC_iron_cross},
73 		{"left_ptr", XC_left_ptr},
74 		{"left_side", XC_left_side},
75 		{"left_tee", XC_left_tee},
76 		{"leftbutton", XC_leftbutton},
77 		{"ll_angle", XC_ll_angle},
78 		{"lr_angle", XC_lr_angle},
79 		{"man", XC_man},
80 		{"middlebutton", XC_middlebutton},
81 		{"mouse", XC_mouse},
82 		{"pencil", XC_pencil},
83 		{"pirate", XC_pirate},
84 		{"plus", XC_plus},
85 		{"question_arrow", XC_question_arrow},
86 		{"right_ptr", XC_right_ptr},
87 		{"right_side", XC_right_side},
88 		{"right_tee", XC_right_tee},
89 		{"rightbutton", XC_rightbutton},
90 		{"rtl_logo", XC_rtl_logo},
91 		{"sailboat", XC_sailboat},
92 		{"sb_down_arrow", XC_sb_down_arrow},
93 		{"sb_h_double_arrow", XC_sb_h_double_arrow},
94 		{"sb_left_arrow", XC_sb_left_arrow},
95 		{"sb_right_arrow", XC_sb_right_arrow},
96 		{"sb_up_arrow", XC_sb_up_arrow},
97 		{"sb_v_double_arrow", XC_sb_v_double_arrow},
98 		{"sizing", XC_sizing},
99 		{"spider", XC_spider},
100 		{"spraycan", XC_spraycan},
101 		{"star", XC_star},
102 		{"target", XC_target},
103 		{"tcross", XC_tcross},
104 		{"top_left_arrow", XC_top_left_arrow},
105 		{"top_left_corner", XC_top_left_corner},
106 		{"top_right_corner", XC_top_right_corner},
107 		{"top_side", XC_top_side},
108 		{"top_tee", XC_top_tee},
109 		{"trek", XC_trek},
110 		{"ul_angle", XC_ul_angle},
111 		{"umbrella", XC_umbrella},
112 		{"ur_angle", XC_ur_angle},
113 		{"watch", XC_watch},
114 		{"x_cursor", XC_X_cursor},
115 		{"xterm", XC_xterm},
116 	};
117 
118 	int cond;
119 	int down = 0;
120 	int up = (sizeof cursor_table / sizeof cursor_table[0]) - 1;
121 	int middle;
122 	char temp[20];
123 	char *s;
124 
125 	if (!cursor_name || cursor_name[0] == 0 ||
126 	    strlen(cursor_name) >= sizeof temp)
127 		return -1;
128 	strcpy(temp, cursor_name);
129 
130 	for (s = temp; *s != 0; s++)
131 		if (isupper(*s))
132 			*s = tolower(*s);
133 
134 	while (down <= up)
135 	{
136 		middle= (down + up) / 2;
137 		if ((cond = strcmp(temp, cursor_table[middle].name)) < 0)
138 			up = middle - 1;
139 		else if (cond > 0)
140 			down =  middle + 1;
141 		else
142 			return cursor_table[middle].number;
143 	}
144 	return -1;
145 }
146