1 /*
2  *  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
3  *
4  *  This is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This software is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this software; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
17  *  USA.
18  */
19 
20 /*
21  * dialog.c - code to deal with dialog boxes.
22  */
23 
24 #include "vncviewer.h"
25 #include <X11/Xaw/Dialog.h>
26 
27 static Bool serverDialogDone = False;
28 static Bool userDialogDone = False;
29 static Bool passwordDialogDone = False;
30 static Bool ycropDialogDone = False;
31 static Bool scaleDialogDone = False;
32 static Bool escapeDialogDone = False;
33 static Bool scbarDialogDone = False;
34 static Bool scaleNDialogDone = False;
35 static Bool qualityDialogDone = False;
36 static Bool compressDialogDone = False;
37 
38 extern void popupFixer(Widget wid);
39 
use_tty(void)40 int use_tty(void) {
41 	if (appData.notty) {
42 		return 0;
43 	} else if (!isatty(0)) {
44 		return 0;
45 	}
46 	return 1;
47 }
48 
49 void
ScaleDialogDone(Widget w,XEvent * event,String * params,Cardinal * num_params)50 ScaleDialogDone(Widget w, XEvent *event, String *params, Cardinal *num_params)
51 {
52 	scaleDialogDone = True;
53 	if (w || event || params || num_params) {}
54 }
55 
56 void
EscapeDialogDone(Widget w,XEvent * event,String * params,Cardinal * num_params)57 EscapeDialogDone(Widget w, XEvent *event, String *params, Cardinal *num_params)
58 {
59 	escapeDialogDone = True;
60 	if (w || event || params || num_params) {}
61 }
62 
dialog_over(Widget wid)63 void dialog_over(Widget wid) {
64 	if (appData.fullScreen) {
65 		if (!net_wm_supported()) {
66 			XtVaSetValues(wid, XtNoverrideRedirect, True, NULL);
67 			XSync(dpy, True);
68 		}
69 	}
70 }
71 
72 extern int XError_ign;
73 
dialog_input(Widget wid)74 void dialog_input(Widget wid) {
75 	XError_ign = 1;
76 	XSetInputFocus(dpy, XtWindow(wid), RevertToParent, CurrentTime);
77 	XSync(dpy, False);
78 	usleep(30 * 1000);
79 	XSync(dpy, False);
80 	usleep(20 * 1000);
81 	XSync(dpy, False);
82 	XError_ign = 0;
83 }
84 
rmNL(char * s)85 static void rmNL(char *s) {
86 	int len;
87 	if (s == NULL) {
88 		return;
89 	}
90 	len = strlen(s);
91 	if (len > 0 && s[len-1] == '\n') {
92 		s[len-1] = '\0';
93 	}
94 }
95 
wm_delete(Widget w,char * func)96 static void wm_delete(Widget w, char *func) {
97 	char str[1024];
98 	Atom wmDeleteWindow = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
99 	XSetWMProtocols(dpy, XtWindow(w), &wmDeleteWindow, 1);
100 	if (func) {
101 		sprintf(str, "<Message>WM_PROTOCOLS: %s", func);
102 		XtOverrideTranslations(w, XtParseTranslationTable (str));
103 	}
104 }
105 
xtmove(Widget w)106 static void xtmove(Widget w) {
107 	XtMoveWidget(w, WidthOfScreen(XtScreen(w))*2/5, HeightOfScreen(XtScreen(w))*2/5);
108 }
109 
110 char *
DoScaleDialog()111 DoScaleDialog()
112 {
113 	Widget pshell, dialog;
114 	char *scaleValue;
115 	char *valueString;
116 
117 	pshell = XtVaCreatePopupShell("scaleDialog", transientShellWidgetClass,
118 				toplevel, NULL);
119 	dialog = XtVaCreateManagedWidget("dialog", dialogWidgetClass, pshell, NULL);
120 
121 	dialog_over(pshell);
122 
123 	if (0) XtMoveWidget(pshell, WidthOfScreen(XtScreen(pshell))*2/5, HeightOfScreen(XtScreen(pshell))*2/5);
124 	XtPopup(pshell, XtGrabNonexclusive);
125 	XtRealizeWidget(pshell);
126 
127 	if (appData.scale != NULL) {
128 		String label;
129 		char tmp[410];
130 		XtVaGetValues(dialog, XtNlabel, &label, NULL);
131 		if (strlen(label) + strlen(appData.scale) < 400) {
132 			sprintf(tmp, "%s %s", label, appData.scale);
133 			XtVaSetValues(dialog, XtNlabel, tmp, NULL);
134 		}
135 	}
136 
137 
138 	if (1 && appData.popupFix) {
139 		popupFixer(pshell);
140 	} else {
141 		xtmove(pshell);
142 	}
143 	dialog_input(pshell);
144 	wm_delete(pshell, "ScaleDialogDone()");
145 
146 	scaleDialogDone = False;
147 
148 	while (!scaleDialogDone) {
149 		XtAppProcessEvent(appContext, XtIMAll);
150 	}
151 
152 	valueString = XawDialogGetValueString(dialog);
153 	rmNL(valueString);
154 	scaleValue = XtNewString(valueString);
155 
156 	XtPopdown(pshell);
157 	return scaleValue;
158 }
159 
160 char *
DoEscapeKeysDialog()161 DoEscapeKeysDialog()
162 {
163 	Widget pshell, dialog;
164 	char *escapeValue;
165 	char *valueString;
166 	char *curr = appData.escapeKeys ? appData.escapeKeys : "default";
167 
168 	pshell = XtVaCreatePopupShell("escapeDialog", transientShellWidgetClass,
169 				toplevel, NULL);
170 	dialog = XtVaCreateManagedWidget("dialog", dialogWidgetClass, pshell, NULL);
171 
172 	dialog_over(pshell);
173 
174 	if(0) XtMoveWidget(pshell, WidthOfScreen(XtScreen(pshell))*2/5, HeightOfScreen(XtScreen(pshell))*2/5);
175 	XtPopup(pshell, XtGrabNonexclusive);
176 	XtRealizeWidget(pshell);
177 
178 	if (curr != NULL) {
179 		String label;
180 		char tmp[3010];
181 		XtVaGetValues(dialog, XtNlabel, &label, NULL);
182 		if (strlen(label) + strlen(curr) < 3000) {
183 			sprintf(tmp, "%s %s", label, curr);
184 			XtVaSetValues(dialog, XtNlabel, tmp, NULL);
185 		}
186 	}
187 
188 	if (appData.popupFix) {
189 		popupFixer(pshell);
190 	} else {
191 		/* too big */
192 		if (0) xtmove(pshell);
193 	}
194 	dialog_input(pshell);
195 	wm_delete(pshell, "EscapeDialogDone()");
196 
197 	escapeDialogDone = False;
198 
199 	while (!escapeDialogDone) {
200 		XtAppProcessEvent(appContext, XtIMAll);
201 	}
202 
203 	valueString = XawDialogGetValueString(dialog);
204 	rmNL(valueString);
205 	escapeValue = XtNewString(valueString);
206 
207 	XtPopdown(pshell);
208 	return escapeValue;
209 }
210 
211 void
YCropDialogDone(Widget w,XEvent * event,String * params,Cardinal * num_params)212 YCropDialogDone(Widget w, XEvent *event, String *params, Cardinal *num_params)
213 {
214 	ycropDialogDone = True;
215 	if (w || event || params || num_params) {}
216 }
217 
218 char *
DoYCropDialog()219 DoYCropDialog()
220 {
221 	Widget pshell, dialog;
222 	char *ycropValue;
223 	char *valueString;
224 
225 	pshell = XtVaCreatePopupShell("ycropDialog", transientShellWidgetClass,
226 				toplevel, NULL);
227 	dialog = XtVaCreateManagedWidget("dialog", dialogWidgetClass, pshell, NULL);
228 
229 	dialog_over(pshell);
230 
231 	if(0) XtMoveWidget(pshell, WidthOfScreen(XtScreen(pshell))*2/5, HeightOfScreen(XtScreen(pshell))*2/5);
232 	XtPopup(pshell, XtGrabNonexclusive);
233 	XtRealizeWidget(pshell);
234 
235 	if (1 && appData.popupFix) {
236 		popupFixer(pshell);
237 	} else {
238 		xtmove(pshell);
239 	}
240 	dialog_input(pshell);
241 	wm_delete(pshell, "YCropDialogDone()");
242 
243 	ycropDialogDone = False;
244 
245 	while (!ycropDialogDone) {
246 		XtAppProcessEvent(appContext, XtIMAll);
247 	}
248 
249 	valueString = XawDialogGetValueString(dialog);
250 	rmNL(valueString);
251 	ycropValue = XtNewString(valueString);
252 
253 	XtPopdown(pshell);
254 	return ycropValue;
255 }
256 
257 void
ScbarDialogDone(Widget w,XEvent * event,String * params,Cardinal * num_params)258 ScbarDialogDone(Widget w, XEvent *event, String *params, Cardinal *num_params)
259 {
260 	scbarDialogDone = True;
261 	if (w || event || params || num_params) {}
262 }
263 
264 char *
DoScbarDialog()265 DoScbarDialog()
266 {
267 	Widget pshell, dialog;
268 	char *scbarValue;
269 	char *valueString;
270 
271 	pshell = XtVaCreatePopupShell("scbarDialog", transientShellWidgetClass,
272 				toplevel, NULL);
273 	dialog = XtVaCreateManagedWidget("dialog", dialogWidgetClass, pshell, NULL);
274 
275 	dialog_over(pshell);
276 
277 	if(0) XtMoveWidget(pshell, WidthOfScreen(XtScreen(pshell))*2/5, HeightOfScreen(XtScreen(pshell))*2/5);
278 	XtPopup(pshell, XtGrabNonexclusive);
279 	XtRealizeWidget(pshell);
280 
281 	if (1 && appData.popupFix) {
282 		popupFixer(pshell);
283 	} else {
284 		xtmove(pshell);
285 	}
286 	dialog_input(pshell);
287 	wm_delete(pshell, "ScbarDialogDone()");
288 
289 	scbarDialogDone = False;
290 
291 	while (!scbarDialogDone) {
292 		XtAppProcessEvent(appContext, XtIMAll);
293 	}
294 
295 	valueString = XawDialogGetValueString(dialog);
296 	rmNL(valueString);
297 	scbarValue = XtNewString(valueString);
298 
299 	XtPopdown(pshell);
300 	return scbarValue;
301 }
302 
303 void
ScaleNDialogDone(Widget w,XEvent * event,String * params,Cardinal * num_params)304 ScaleNDialogDone(Widget w, XEvent *event, String *params, Cardinal *num_params)
305 {
306 	scaleNDialogDone = True;
307 	if (w || event || params || num_params) {}
308 }
309 
310 char *
DoScaleNDialog()311 DoScaleNDialog()
312 {
313 	Widget pshell, dialog;
314 	char *scaleNValue;
315 	char *valueString;
316 
317 	pshell = XtVaCreatePopupShell("scaleNDialog", transientShellWidgetClass,
318 				toplevel, NULL);
319 	dialog = XtVaCreateManagedWidget("dialog", dialogWidgetClass, pshell, NULL);
320 
321 	dialog_over(pshell);
322 	wm_delete(pshell, "ScaleNDialogDone()");
323 
324 	if(0) XtMoveWidget(pshell, WidthOfScreen(XtScreen(pshell))*2/5, HeightOfScreen(XtScreen(pshell))*2/5);
325 	XtPopup(pshell, XtGrabNonexclusive);
326 	XtRealizeWidget(pshell);
327 
328 	if (appData.popupFix) {
329 		popupFixer(pshell);
330 	} else {
331 		xtmove(pshell);
332 	}
333 	dialog_input(pshell);
334 	wm_delete(pshell, "ScaleNDialogDone()");
335 
336 	scaleNDialogDone = False;
337 
338 	while (!scaleNDialogDone) {
339 		XtAppProcessEvent(appContext, XtIMAll);
340 	}
341 
342 	valueString = XawDialogGetValueString(dialog);
343 	rmNL(valueString);
344 	scaleNValue = XtNewString(valueString);
345 
346 	XtPopdown(pshell);
347 	return scaleNValue;
348 }
349 
350 void
QualityDialogDone(Widget w,XEvent * event,String * params,Cardinal * num_params)351 QualityDialogDone(Widget w, XEvent *event, String *params, Cardinal *num_params)
352 {
353 	qualityDialogDone = True;
354 	if (w || event || params || num_params) {}
355 }
356 
357 char *
DoQualityDialog()358 DoQualityDialog()
359 {
360 	Widget pshell, dialog;
361 	char *qualityValue;
362 	char *valueString;
363 
364 	pshell = XtVaCreatePopupShell("qualityDialog", transientShellWidgetClass,
365 				toplevel, NULL);
366 	dialog = XtVaCreateManagedWidget("dialog", dialogWidgetClass, pshell, NULL);
367 
368 	dialog_over(pshell);
369 
370 	if(0) XtMoveWidget(pshell, WidthOfScreen(XtScreen(pshell))*2/5, HeightOfScreen(XtScreen(pshell))*2/5);
371 	XtPopup(pshell, XtGrabNonexclusive);
372 	XtRealizeWidget(pshell);
373 
374 	if (1 && appData.popupFix) {
375 		popupFixer(pshell);
376 	} else {
377 		xtmove(pshell);
378 	}
379 	dialog_input(pshell);
380 	wm_delete(pshell, "QualityDialogDone() HideQuality()");
381 
382 	qualityDialogDone = False;
383 
384 	while (!qualityDialogDone) {
385 		XtAppProcessEvent(appContext, XtIMAll);
386 	}
387 
388 	valueString = XawDialogGetValueString(dialog);
389 	rmNL(valueString);
390 	qualityValue = XtNewString(valueString);
391 
392 	XtPopdown(pshell);
393 	return qualityValue;
394 }
395 
396 void
CompressDialogDone(Widget w,XEvent * event,String * params,Cardinal * num_params)397 CompressDialogDone(Widget w, XEvent *event, String *params, Cardinal *num_params)
398 {
399 	compressDialogDone = True;
400 	if (w || event || params || num_params) {}
401 }
402 
403 char *
DoCompressDialog()404 DoCompressDialog()
405 {
406 	Widget pshell, dialog;
407 	char *compressValue;
408 	char *valueString;
409 
410 	fprintf(stderr, "compress start:\n");
411 
412 	pshell = XtVaCreatePopupShell("compressDialog", transientShellWidgetClass,
413 				toplevel, NULL);
414 	dialog = XtVaCreateManagedWidget("dialog", dialogWidgetClass, pshell, NULL);
415 
416 	dialog_over(pshell);
417 
418 	if(0) XtMoveWidget(pshell, WidthOfScreen(XtScreen(pshell))*2/5, HeightOfScreen(XtScreen(pshell))*2/5);
419 	XtPopup(pshell, XtGrabNonexclusive);
420 	XtRealizeWidget(pshell);
421 
422 	if (1 && appData.popupFix) {
423 		popupFixer(pshell);
424 	} else {
425 		xtmove(pshell);
426 	}
427 	dialog_input(pshell);
428 	wm_delete(pshell, "CompressDialogDone() HideCompress()");
429 
430 	compressDialogDone = False;
431 
432 	while (!compressDialogDone) {
433 		XtAppProcessEvent(appContext, XtIMAll);
434 	}
435 
436 	valueString = XawDialogGetValueString(dialog);
437 	rmNL(valueString);
438 	compressValue = XtNewString(valueString);
439 
440 	fprintf(stderr, "compress done: %s\n", compressValue);
441 
442 	XtPopdown(pshell);
443 	return compressValue;
444 }
445 
446 void
ServerDialogDone(Widget w,XEvent * event,String * params,Cardinal * num_params)447 ServerDialogDone(Widget w, XEvent *event, String *params, Cardinal *num_params)
448 {
449 	serverDialogDone = True;
450 	if (w || event || params || num_params) {}
451 }
452 
453 char *
DoServerDialog()454 DoServerDialog()
455 {
456 	Widget pshell, dialog;
457 	char *vncServerName;
458 	char *valueString;
459 
460 	pshell = XtVaCreatePopupShell("serverDialog", transientShellWidgetClass,
461 				toplevel, NULL);
462 	dialog = XtVaCreateManagedWidget("dialog", dialogWidgetClass, pshell, NULL);
463 
464 	dialog_over(pshell);
465 
466 	if (0) XtMoveWidget(pshell, WidthOfScreen(XtScreen(pshell))*2/5, HeightOfScreen(XtScreen(pshell))*2/5);
467 	XtPopup(pshell, XtGrabNonexclusive);
468 	XtRealizeWidget(pshell);
469 
470 	if (0 && appData.popupFix) {
471 		popupFixer(pshell);
472 	} else {
473 		xtmove(pshell);
474 	}
475 #if 0
476 	dialog_input(pshell);
477 #endif
478 	wm_delete(pshell, "ServerDialogDone()");
479 
480 	serverDialogDone = False;
481 
482 	while (!serverDialogDone) {
483 		XtAppProcessEvent(appContext, XtIMAll);
484 	}
485 
486 	valueString = XawDialogGetValueString(dialog);
487 	rmNL(valueString);
488 	vncServerName = XtNewString(valueString);
489 
490 	XtPopdown(pshell);
491 	return vncServerName;
492 }
493 
494 void
UserDialogDone(Widget w,XEvent * event,String * params,Cardinal * num_params)495 UserDialogDone(Widget w, XEvent *event, String *params, Cardinal *num_params)
496 {
497 	userDialogDone = True;
498 	if (w || event || params || num_params) {}
499 }
500 
501 char *
DoUserDialog()502 DoUserDialog()
503 {
504 	Widget pshell, dialog;
505 	char *userName;
506 	char *valueString;
507 
508 	pshell = XtVaCreatePopupShell("userDialog", transientShellWidgetClass,
509 				toplevel, NULL);
510 	dialog = XtVaCreateManagedWidget("dialog", dialogWidgetClass, pshell, NULL);
511 
512 	dialog_over(pshell);
513 
514 	if(0) XtMoveWidget(pshell, WidthOfScreen(XtScreen(pshell))*2/5, HeightOfScreen(XtScreen(pshell))*2/5);
515 	XtPopup(pshell, XtGrabNonexclusive);
516 	XtRealizeWidget(pshell);
517 
518 	if (0 && appData.popupFix) {
519 		popupFixer(pshell);
520 	} else {
521 		xtmove(pshell);
522 	}
523 #if 0
524 	dialog_input(pshell);
525 #endif
526 	wm_delete(pshell, "UserDialogDone()");
527 
528 	userDialogDone = False;
529 
530 	while (!userDialogDone) {
531 		XtAppProcessEvent(appContext, XtIMAll);
532 	}
533 
534 	valueString = XawDialogGetValueString(dialog);
535 	rmNL(valueString);
536 	userName = XtNewString(valueString);
537 
538 	XtPopdown(pshell);
539 	return userName;
540 }
541 
542 void
PasswordDialogDone(Widget w,XEvent * event,String * params,Cardinal * num_params)543 PasswordDialogDone(Widget w, XEvent *event, String *params, Cardinal *num_params)
544 {
545 	passwordDialogDone = True;
546 	if (w || event || params || num_params) {}
547 }
548 
549 char *
DoPasswordDialog()550 DoPasswordDialog()
551 {
552 	Widget pshell, dialog;
553 	char *password;
554 	char *valueString;
555 
556 	pshell = XtVaCreatePopupShell("passwordDialog", transientShellWidgetClass,
557 				toplevel, NULL);
558 	dialog = XtVaCreateManagedWidget("dialog", dialogWidgetClass, pshell, NULL);
559 
560 	dialog_over(pshell);
561 
562 	if(0) XtMoveWidget(pshell, WidthOfScreen(XtScreen(pshell))*2/5, HeightOfScreen(XtScreen(pshell))*2/5);
563 	XtPopup(pshell, XtGrabNonexclusive);
564 	XtRealizeWidget(pshell);
565 
566 	if (0 && appData.popupFix) {
567 		popupFixer(pshell);
568 	} else {
569 		xtmove(pshell);
570 	}
571 #if 0
572 	dialog_input(pshell);
573 #endif
574 	wm_delete(pshell, "PasswordDialogDone()");
575 
576 	passwordDialogDone = False;
577 
578 	while (!passwordDialogDone) {
579 		XtAppProcessEvent(appContext, XtIMAll);
580 	}
581 
582 	valueString = XawDialogGetValueString(dialog);
583 	rmNL(valueString);
584 	password = XtNewString(valueString);
585 
586 	XtPopdown(pshell);
587 	return password;
588 }
589