1 
2 /*
3  *
4 Copyright 1989, 1998  The Open Group
5 
6 Permission to use, copy, modify, distribute, and sell this software and its
7 documentation for any purpose is hereby granted without fee, provided that
8 the above copyright notice appear in all copies and that both that
9 copyright notice and this permission notice appear in supporting
10 documentation.
11 
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14 
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 Except as contained in this notice, the name of The Open Group shall not be
23 used in advertising or otherwise to promote the sale, use or other dealings
24 in this Software without prior written authorization from The Open Group.
25  *
26  * Author:  Chris D. Peterson, MIT X Consortium
27  */
28 
29 #include <X11/Xmu/EditresP.h>
30 #include <X11/Xresource.h>
31 #include <X11/Xfuncproto.h>
32 
33 #define DEBUG
34 
35 #ifdef DEBUG
36 #  define CLIENT_TIME_OUT 60000	/* wait sixty seconds for the client. */
37 #else
38 #  define CLIENT_TIME_OUT 5000	/* wait five seconds for the client. */
39 #endif /* DEBUG */
40 
41 #define PROTOCOL_VERSION_ONE_POINT_ONE  5 /* version 1.1 */
42 #define ONE_POINT_ONE_STRING "1.1"
43 #define PROTOCOL_VERSION_ONE_POINT_ZERO 4 /* version 1.0 */
44 #define ONE_POINT_ZERO_STRING "1.0" ONE_POINT_ONE_STRING
45 
46 #define CURRENT_PROTOCOL_VERSION PROTOCOL_VERSION_ONE_POINT_ONE
47 #define CURRENT_PROTOCOL_VERSION_STRING ONE_POINT_ONE_STRING
48 
49 #define FLASH_TIME  1000	/* Default flash time in microseconds */
50 #define NUM_FLASHES 3		/* Default number of flashes. */
51 
52 #define NO_IDENT 0		/* an ident that will match nothing. */
53 
54 #define NUM_INC 10		/* amount to increment allocators. */
55 
56 #define ANY_RADIO_DATA ("the any widget")
57 #define RESOURCE_BOX ("resourceBox")
58 
59 
60 /*
61  * Retrieving ResType and Boolean is the same as retrieving a Card8 except
62  * possibly for signedness.
63  */
64 
65 #define _XEditResGetBoolean(_s, _r) _XEditResGet8((_s), (unsigned char *)(_r))
66 #define _XEditResGetResType _XEditResGet8
67 
68 /*
69  * Contexts to use with the X Context Manager.
70  */
71 
72 #define NODE_INFO ((XContext) 42)
73 
74 /*
75  * Error codes for X Server errors.
76  */
77 
78 #define NO_ERROR 0
79 #define NO_WINDOW 1
80 
81 typedef enum {LocalSendWidgetTree, LocalSetValues, LocalFindChild,
82 	      LocalFlashWidget, LocalGetGeometry, LocalGetResources,
83               LocalGetValues} ResCommand;
84 
85 typedef enum {ClassLabel, NameLabel, IDLabel, WindowLabel,
86 	      ToggleLabel} LabelTypes;
87 typedef enum {SelectWidget, SelectAll, SelectNone, SelectInvert, SelectParent,
88 	      SelectChildren,  SelectDescendants, SelectAncestors} SelectTypes;
89 
90 typedef struct _NameInfo {
91     struct _NameInfo * next;	/* Next element in the linked list. */
92     Widget sep_leader;		/* The separator toggle group leader. */
93     Widget name_leader;		/* The name toggle group leader. */
94 } NameInfo;
95 
96 typedef struct _ResourceBoxInfo {
97     Widget value_wid;		/* The string containing the value. */
98     Widget res_label;		/* The label containing current resoruce. */
99     Widget shell;		/* Shell widget containing resource box. */
100     Widget norm_list;		/* The List widget for the normal list. */
101     Widget cons_list;		/* The List widget for the
102 				   Constriaint Resources */
103     NameInfo * name_info;	/* The info about the widgets for each
104 				   name and class in the instance heirarchy. */
105 } ResourceBoxInfo;
106 
107 typedef struct _WidgetResourceInfo {
108     char * name, * class, *type; /* Name, Class and Type of each resource. */
109 } WidgetResourceInfo;
110 
111 typedef struct _WidgetResources {
112     int num_normal, num_constraint;
113     WidgetResourceInfo *normal, *constraint;
114     ResourceBoxInfo * res_box;
115 } WidgetResources;
116 
117 typedef struct _WNode {
118     char * name;
119     char * class;
120     unsigned long id, window;
121     struct _WNode * parent;
122     struct _WNode ** children;
123     struct _TreeInfo * tree_info;
124     Cardinal num_children, alloc_children;
125     Widget widget;
126     WidgetResources * resources;
127 } WNode;
128 
129 /*
130  * Information for the Select any widget, toggle buttons in the resource
131  * boxes.
132  */
133 
134 typedef struct _AnyInfo {
135     WNode * node;		/* A Pointer off to the node corrsponding to
136 				   this resource box. */
137     Widget left_dot, left_star;	/* The dot and star widgets to our left. */
138     Widget right_dot, right_star; /* The dot and star widgets to our right. */
139     int left_count, *right_count; /* If count > 0 then desensitize the left or
140 				    right dot and star widgets. */
141 } AnyInfo;
142 
143 /*
144  * Information about the client we are currently working with.
145  */
146 
147 typedef struct _CurrentClient {
148     ResCommand command;		/* the command sent. */
149     ResIdent ident;
150     ProtocolStream stream;	/* protocol stream for this client. */
151     XtIntervalId timeout;	/* timeout set in case he doesn't answer. */
152     Window window;		/* window to communicate with. */
153     Atom atom;			/* Atom used to communicate with this client.*/
154 } CurrentClient;
155 
156 /*
157  * Information about a tree we can display.
158  */
159 
160 typedef struct _TreeInfo {
161     Widget tree_widget;		/* The Tree widget that contains all nodes */
162     WNode * top_node;		/* The top node in the tree. */
163     WNode ** active_nodes;	/* The currently active nodes. */
164     Cardinal num_nodes, alloc_nodes; /* number of active nodes, and space */
165     Widget * flash_widgets;	/* list of widgets to flash on and off. */
166     Cardinal num_flash_widgets, alloc_flash_widgets; /* number of flash wids.*/
167 } TreeInfo;
168 
169 /*
170  * Information specific to a give APPLICATION screen.
171  */
172 
173 typedef struct _ScreenData {
174     Widget set_values_popup;	/* The SetValues popup. */
175     Widget res_text;		/* SetValues resource text widget. */
176     Widget val_text;		/* SetValues value text widget. */
177     Widget info_label;	        /* The information label. */
178 } ScreenData;
179 
180 typedef struct _AppResources {
181     Boolean debug;		/* Is debugging on? */
182     int num_flashes, flash_time; /* Number and duration of flashes. */
183     Pixel flash_color;		/* Color of flash window. */
184     char * save_resources_file;	/* File to save the resources into. */
185 
186     /* Private state */
187     Boolean allocated_save_resources_file;
188 } AppResources;
189 
190 /*
191  * Information needed to apply the resource string to all widgets.
192  */
193 
194 typedef struct _ApplyResourcesInfo {
195     char * name, *class;	/* name and class  of this resource. */
196     unsigned short count;
197     ProtocolStream * stream;
198     XrmDatabase database;
199 } ApplyResourcesInfo;
200 
201 /*
202  * Information needed to get a resource string from a widget.
203  */
204 
205 typedef struct _ObtainResourcesInfo {
206     char * name, *class;	/* name and class  of this resource. */
207     unsigned short count;
208     ProtocolStream * stream;
209     XrmDatabase database;
210 } ObtainResourcesInfo;
211 
212 /************************************************************
213  *
214  * The Event Structures.
215  *
216  ************************************************************/
217 
218 typedef struct _AnyEvent {
219     EditresCommand type;
220 } AnyEvent;
221 
222 typedef struct _WidgetTreeInfo {
223     WidgetInfo widgets;
224     char * name;
225     char * class;
226     unsigned long window;
227 } WidgetTreeInfo;
228 
229 typedef struct _SendWidgetTreeEvent {
230     EditresCommand type;
231     char * toolkit;
232     unsigned short num_entries;
233     WidgetTreeInfo * info;
234 } SendWidgetTreeEvent;
235 
236 typedef struct _SetValuesInfo {
237     WidgetInfo widgets;
238     char * message;
239 } SetValuesInfo;
240 
241 typedef struct _SetValuesEvent {
242     EditresCommand type;
243     unsigned short num_entries;
244     SetValuesInfo * info;
245 } SetValuesEvent;
246 
247 typedef struct _GetValuesInfo {
248     WidgetInfo widgets;
249     char * value;
250 } GetValuesInfo;
251 
252 typedef struct _GetValuesEvent {
253     EditresCommand type;
254     unsigned short num_entries;
255     GetValuesInfo * info;
256 } GetValuesEvent;
257 
258 typedef struct _ResourceInfo {
259     ResourceType res_type;
260     char * name, *class, *type;
261 } ResourceInfo;
262 
263 typedef struct _GetResourcesInfo {
264     WidgetInfo widgets;
265     Boolean error;
266     char * message;
267     unsigned short num_resources;
268     ResourceInfo * res_info;
269 } GetResourcesInfo;
270 
271 typedef struct _GetResourcesEvent {
272     EditresCommand type;
273     unsigned short num_entries;
274     GetResourcesInfo * info;
275 } GetResourcesEvent;
276 
277 typedef struct _GetGeomInfo {
278     EditresCommand type;
279     WidgetInfo widgets;
280     Boolean error;
281     char * message;
282     Boolean visable;
283     short x, y;
284     unsigned short width, height, border_width;
285 } GetGeomInfo;
286 
287 typedef struct _GetGeomEvent {
288     EditresCommand type;
289     unsigned short num_entries;
290     GetGeomInfo * info;
291 } GetGeomEvent;
292 
293 typedef struct _FindChildEvent {
294     EditresCommand type;
295     WidgetInfo widgets;
296 } FindChildEvent;
297 
298 typedef union _Event {
299     AnyEvent any_event;
300     SendWidgetTreeEvent send_widget_tree_event;
301     SetValuesEvent set_values_event;
302     GetResourcesEvent get_resources_event;
303     GetGeomEvent get_geom_event;
304     FindChildEvent find_child_event;
305     GetValuesEvent get_values_event;
306 } Event;
307 
308 /*
309  * number of application resource labels.
310  */
311 
312 #define NUM_RES_LABELS 37
313 
314 /*
315  * Global variables.
316  */
317 extern int global_effective_protocol_version;
318 extern char* global_effective_toolkit;
319 extern int global_error_code;
320 extern unsigned long global_serial_num;
321 extern int (*global_old_error_handler)(Display *, XErrorEvent *);
322 extern Boolean global_resource_box_up;
323 
324 extern TreeInfo *global_tree_info;
325 extern CurrentClient global_client;
326 extern ScreenData global_screen_data;
327 extern Widget global_tree_parent;
328 extern Widget global_paned;	/* named after toolkit */
329 extern Widget global_toplevel;
330 extern AppResources global_resources;
331 
332 extern String res_labels[NUM_RES_LABELS];
333 
334 extern Boolean do_get_values;
335 extern Atom wm_delete_window;
336 
337 /* number of entries in the command menu */
338 #define NUM_CM_ENTRIES 8
339 extern Widget CM_entries[NUM_CM_ENTRIES];
340 
341 /* number of entries in the tree menu */
342 #define NUM_TM_ENTRIES 16
343 extern Widget TM_entries[NUM_TM_ENTRIES];
344 
345 /*
346  * Macros.
347  */
348 
349 #define streq(a, b)        ( strcmp((a), (b)) == 0 )
350 
351 /* offset into CM entries for setting insensitive */
352 #define CM_OFFSET 1
353 /* number of CM entries to make insensitive */
354 #define CM_NUM 5
355 #define TM_OFFSET 0
356 #define TM_NUM 16
357 
358 /*
359  * Prototypes
360  */
361 extern void ActivateResourceWidgets ( Widget w, XtPointer node_ptr, XtPointer junk );
362 extern void ActivateWidgetsAndSetResourceString ( Widget w, XtPointer node_ptr, XtPointer call_data );
363 extern void AddString ( char ** str, char *add );
364 extern void AddTreeNode ( Widget tree, WNode * top );
365 extern void AnyChosen ( Widget w, XtPointer any_info_ptr, XtPointer state_ptr );
366 extern void ApplyResource ( Widget w, XtPointer node_ptr, XtPointer junk );
367 extern void BuildVisualTree ( Widget tree_parent, Event * event );
368 extern void BuildWidgetTree ( Widget parent );
369 extern Boolean CheckDatabase ( XrmDatabase db, XrmQuarkList names, XrmQuarkList classes );
370 extern void CreateResourceBox ( WNode * node, char ** errors );
371 extern void CreateResourceBoxWidgets ( WNode * node, char **names, char **cons_names );
372 extern TreeInfo * CreateTree ( Event * event );
373 extern void DisplayChild ( Event * event );
374 extern void DumpTreeToFile ( Widget w, XtPointer junk, XtPointer garbage );
375 extern void ExecuteOverAllNodes ( WNode * top_node, void (*func)(WNode *, XtPointer), XtPointer data );
376 extern WNode * FindNode ( WNode *top_node, unsigned long * ids, Cardinal number );
377 extern void FindWidget ( Widget w, XtPointer client_data, XtPointer call_data );
378 extern WNode * FindWidgetFromWindow ( TreeInfo * tree_info, Window win );
379 extern void FlashActiveWidgets ( Widget w, XtPointer junk, XtPointer garbage );
380 extern void GetAllStrings ( char *in, char sep, char ***out, int * num );
381 extern Window GetClientWindow ( Widget w, int *x, int *y );
382 extern char * GetFailureMessage ( ProtocolStream * stream );
383 extern void GetNamesAndClasses ( WNode * node, char *** names, char ***classes );
384 extern ResIdent GetNewIdent ( void );
385 extern void GetResourceList ( Widget w, XtPointer junk, XtPointer garbage );
386 extern char * GetResourceValueForSetValues ( WNode * node, unsigned short * size );
387 extern char * HandleFlashWidget ( Event * event );
388 extern char * HandleGetResources ( Event * event );
389 extern int HandleXErrors ( Display * display, XErrorEvent * error );
390 extern void InitSetValues ( Widget w, XtPointer client_data, XtPointer call_data );
391 extern void InsertWidgetFromNode ( ProtocolStream * stream, WNode * node );
392 extern void InternAtoms ( Display * dpy );
393 extern void LayoutTree ( Widget tree );
394 extern int main ( int argc, char **argv );
395 extern void ModifySVEntry ( Widget w, XEvent *event, String * params, Cardinal * num_params );
396 extern void PannerCallback ( Widget w, XtPointer closure, XtPointer report_ptr );
397 extern void PerformTreeToFileDump ( WNode * node, Cardinal num_tabs, FILE * fp );
398 extern void PopdownResBox ( Widget w, XtPointer shell_ptr, XtPointer junk );
399 extern void PopupCentered ( XEvent * event, Widget w, XtGrabKind mode );
400 extern void PopupSetValues ( Widget parent, XEvent * event );
401 extern void PortholeCallback ( Widget w, XtPointer panner_ptr, XtPointer report_ptr );
402 extern void PrepareToLayoutTree ( Widget tree );
403 extern void PrintNodes ( WNode * top );
404 extern char * PrintSetValuesError ( Event * event );
405 extern char * ProtocolFailure ( ProtocolStream * stream );
406 extern XrmQuarkList Quarkify ( char ** list, char * ptr );
407 extern void Quit ( Widget w, XtPointer client_data, XtPointer call_data ) _X_NORETURN;
408 extern void RebuildMenusAndLabel ( String toolkit );
409 extern void ResourceListCallback ( Widget list, XtPointer node_ptr, XtPointer junk );
410 extern void SaveResource ( Widget w, XtPointer res_box_ptr, XtPointer junk );
411 extern void SendTree ( Widget w, XtPointer value, XtPointer call_data );
412 extern void SetAndCenterTreeNode ( WNode * node );
413 extern void SetApplicationActions ( XtAppContext app_con );
414 extern void SetCommand ( Widget w, ResCommand command, char * msg );
415 extern void SetEntriesSensitive ( Widget *entries, int num, Boolean sensitive );
416 extern void SetFile ( Widget w, XtPointer junk, XtPointer garbage );
417 extern void SetMessage ( Widget w, char * str );
418 extern void SetResourceString ( Widget w, XtPointer node_ptr, XtPointer junk );
419 extern void TreeRelabel ( Widget w, XtPointer client_data, XtPointer call_data );
420 extern void TreeSelect ( Widget w, XtPointer client_data, XtPointer call_data );
421 extern void TreeToggle ( Widget w, XtPointer node_ptr, XtPointer state_ptr );
422 extern void _DumpTreeToFile ( Widget w, XtPointer tree_ptr, XtPointer filename );
423 extern void _FindWidget ( Widget w );
424 extern void _FlashActiveWidgets ( TreeInfo * tree_info );
425 extern void _PopdownFileDialog ( Widget w, XtPointer client_data, XtPointer junk );
426 extern void _PopupFileDialog ( Widget w, String str, String default_value, XtCallbackProc func, XtPointer data );
427 extern void _TreeActivateNode ( WNode * node, SelectTypes type );
428 extern void _TreeRelabel ( TreeInfo * tree_info, LabelTypes type );
429 extern void _TreeRelabelNode ( WNode * node, LabelTypes type, Boolean recurse );
430 extern void _TreeSelect ( TreeInfo * tree_info, SelectTypes type );
431 extern void _TreeSelectNode ( WNode * node, SelectTypes type, Boolean recurse );
432 
433