1 /* x11-ssh-askpass.h:  A generic X11-based password dialog for OpenSSH.
2  * created 1999-Nov-17 03:40 Jim Knoble <jmknoble@pobox.com>
3  * autodate: 2001-Sep-16 18:08
4  *
5  * by Jim Knoble <jmknoble@pobox.com>
6  * Copyright (C) 1999,2000,2001 Jim Knoble
7  *
8  * Disclaimer:
9  *
10  * The software is provided "as is", without warranty of any kind,
11  * express or implied, including but not limited to the warranties of
12  * merchantability, fitness for a particular purpose and
13  * noninfringement. In no event shall the author(s) be liable for any
14  * claim, damages or other liability, whether in an action of
15  * contract, tort or otherwise, arising from, out of or in connection
16  * with the software or the use or other dealings in the software.
17  *
18  * Portions of this code are distantly derived from code in xscreensaver
19  * by Jamie Zawinski <jwz@jwz.org>.  That code says:
20  *
21  * --------8<------------------------------------------------8<--------
22  * xscreensaver, Copyright (c) 1991-1999 Jamie Zawinski <jwz@jwz.org>
23  *
24  * Permission to use, copy, modify, distribute, and sell this software and its
25  * documentation for any purpose is hereby granted without fee, provided that
26  * the above copyright notice appear in all copies and that both that
27  * copyright notice and this permission notice appear in supporting
28  * documentation.  No representations are made about the suitability of this
29  * software for any purpose.  It is provided "as is" without express or
30  * implied warranty.
31  * --------8<------------------------------------------------8<--------
32  *
33  * The remainder of this code falls under the same permissions and
34  * provisions as those of the xscreensaver code.
35  */
36 
37 #ifndef H_X11_SSH_ASKPASS
38 #define H_X11_SSH_ASKPASS
39 
40 #include <X11/Xlib.h>
41 #include <X11/Intrinsic.h>
42 #include <X11/Shell.h>
43 
44 #define EXIT_STATUS_ACCEPT	0
45 #define EXIT_STATUS_CANCEL	1
46 #define EXIT_STATUS_NO_MEMORY	2
47 #define EXIT_STATUS_ERROR	3
48 #define EXIT_STATUS_TIMEOUT	4
49 #define EXIT_STATUS_ANOMALY	127
50 
51 typedef struct
52 {
53    Pixel foreground;
54    Pixel background;
55    Dimension width;
56    Dimension height;
57    Position x;
58    Position y;
59 } WidgetInfo;
60 
61 typedef struct
62 {
63    WidgetInfo w;
64    Pixel topShadowColor;
65    Pixel bottomShadowColor;
66    Dimension shadowThickness;
67    Pixel borderColor;
68    Dimension borderWidth;
69    Dimension interiorWidth;
70    Dimension interiorHeight;
71    Dimension horizontalSpacing;
72    Dimension verticalSpacing;
73 } Widget3dInfo;
74 
75 typedef struct TextObjectStruct
76 {
77    char *text;
78    int textLength;
79    int direction;
80    int ascent;
81    int descent;
82    XCharStruct overall;
83    struct TextObjectStruct *next;
84 } TextObject;
85 
86 typedef struct
87 {
88    char *fullText;
89    XFontStruct *font;
90    TextObject *multiText;
91    WidgetInfo w;
92 } LabelInfo;
93 
94 typedef struct
95 {
96    Widget3dInfo w3;
97    LabelInfo label;
98    Bool pressed;
99 } ButtonInfo;
100 
101 typedef struct
102 {
103    Widget3dInfo w3;
104    int count;
105    int current;
106    int minimumCount;
107    int maximumCount;
108 } MasterIndicatorInfo;
109 
110 typedef struct
111 {
112    MasterIndicatorInfo *parent;
113    WidgetInfo w;
114    Bool isLit;
115 } IndicatorElement;
116 
117 typedef struct
118 {
119    Window dialogWindow;
120 
121    XSizeHints *sizeHints;
122    XWMHints *wmHints;
123    XClassHint *classHints;
124    XTextProperty windowName;
125 
126    char *title;
127    Widget3dInfo w3;
128 
129    LabelInfo label;
130 
131    MasterIndicatorInfo indicator;
132    IndicatorElement *indicators;
133 
134    ButtonInfo okButton;
135    ButtonInfo cancelButton;
136 
137    int pressedButton;
138 } DialogInfo;
139 
140 #define NO_BUTTON	0
141 #define OK_BUTTON	1
142 #define CANCEL_BUTTON	2
143 
144 typedef struct
145 {
146    char *appName;
147    char *appClass;
148 
149    int argc;
150    char **argv;
151 
152    pid_t pid;
153 
154    char *buf;
155    int bufSize;
156    int bufIndex;
157 
158    Display *dpy;
159    Screen *screen;
160    Window rootWindow;
161    Pixel black;
162    Pixel white;
163    Colormap colormap;
164 
165    /* Resolution measurements are normalized to dots/meter. */
166    long xResolution;
167    long yResolution;
168    long defaultXResolution;
169    long defaultYResolution;
170    long xFuzz;
171    long yFuzz;
172 
173    XtAppContext appContext;
174    Widget toplevelShell;
175    XrmDatabase resourceDb;
176 
177    Atom wmDeleteWindowAtom;
178 
179    GC fillGC;
180    GC borderGC;
181    GC textGC;
182    GC brightGC;
183    GC dimGC;
184 
185    long eventMask;
186 
187    Bool grabKeyboard;
188    Bool grabPointer;
189    Bool grabServer;
190    Bool isKeyboardGrabbed;
191    Bool isPointerGrabbed;
192    Bool isServerGrabbed;
193    unsigned int grabFailTimeout;
194    unsigned int grabRetryInterval;
195 
196    unsigned long inputTimeout;
197    XtIntervalId inputTimeoutTimerId;
198    Bool inputTimeoutActive;
199 
200    DialogInfo *dialog;
201 } AppInfo;
202 
203 void outOfMemory(AppInfo *app, int line);
204 void freeIf(void *p);
205 void freeFontIf(AppInfo *app, XFontStruct *f);
206 
207 XFontStruct *getFontResource(AppInfo *app, char *instanceName, char *className);
208 char *getStringResourceWithDefault(char *instanceName, char *className,
209 				   char *defaultText);
210 unsigned int getUnsignedIntegerResource(AppInfo *app, char *instanceName,
211 					char *className,
212 					unsigned int defaultValue);
213 long getResolutionResource(AppInfo *app, char *instanceName, char *className,
214 			   char *defaultResolutionSpec);
215 
216 void calcLabelTextExtents(LabelInfo *label);
217 void calcTotalButtonExtents(ButtonInfo *button);
218 void calcButtonExtents(ButtonInfo *button);
219 void balanceButtonExtents(ButtonInfo *button1, ButtonInfo *button2);
220 void calcButtonLabelPosition(ButtonInfo *button);
221 
222 Dimension scaleXDimension(AppInfo *app, Dimension unscaled);
223 Dimension scaleYDimension(AppInfo *app, Dimension unscaled);
224 
225 void createDialog(AppInfo *app);
226 void destroyDialog(AppInfo *app);
227 void createDialogWindow(AppInfo *app);
228 void createGCs(AppInfo *app);
229 void destroyGCs(AppInfo *app);
230 
231 void paintLabel(AppInfo *app, Drawable draw, LabelInfo label);
232 void paintButton(AppInfo *app, Drawable draw, ButtonInfo button);
233 void paintIndicator(AppInfo *app, Drawable draw, IndicatorElement indicator);
234 void updateIndicatorElement(AppInfo *app, int i);
235 void updateIndicators(AppInfo *app, int condition);
236 void paintDialog(AppInfo *app);
237 
238 #define GRAB_KEYBOARD	0
239 #define GRAB_POINTER	1
240 void performGrab(AppInfo *app, int grabType, char *grabTypeName,
241 		 Bool shouldGrab, Bool *isGrabbed);
242 
243 void grabKeyboard(AppInfo *app);
244 void ungrabKeyboard(AppInfo *app);
245 void grabPointer(AppInfo *app);
246 void ungrabPointer(AppInfo *app);
247 void grabServer(AppInfo *app);
248 void ungrabServer(AppInfo *app);
249 
250 void cleanUp(AppInfo *app);
251 void exitApp(AppInfo *app, int exitCode);
252 
253 void acceptAction(AppInfo *app);
254 void cancelAction(AppInfo *app);
255 
256 void backspacePassphrase(AppInfo *app);
257 void erasePassphrase(AppInfo *app);
258 void addToPassphrase(AppInfo *app, char c);
259 
260 void handleKeyPress(AppInfo *app, XEvent *event);
261 Bool eventIsInsideButton(AppInfo *app, XEvent *event, ButtonInfo button);
262 void handleButtonPress(AppInfo *app, XEvent *event);
263 void handlePointerMotion(AppInfo *app, XEvent *event);
264 
265 void handleInputTimeout(XtPointer data, XtIntervalId *timerId);
266 void cancelInputTimeout(AppInfo *app);
267 
268 #endif /* H_X11_SSH_ASKPASS */
269