-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathlock.c
2285 lines (1893 loc) · 66.9 KB
/
lock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* lock.c --- handling the password dialog for locking-mode.
* xscreensaver, Copyright (c) 1993-2020 Jamie Zawinski <jwz@jwz.org>
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation. No representations are made about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*/
/* Athena locking code contributed by Jon A. Christopher <jac8782@tamu.edu> */
/* Copyright 1997, with the same permissions as above. */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <ctype.h>
#include <X11/Intrinsic.h>
#include <X11/cursorfont.h>
#include <X11/Xos.h> /* for time() */
#include <time.h>
#include <sys/time.h>
#include "xscreensaver.h"
#include "resources.h"
#include "mlstring.h"
#include "auth.h"
#ifndef NO_LOCKING /* (mostly) whole file */
#ifdef HAVE_XHPDISABLERESET
# include <X11/XHPlib.h>
static void hp_lock_reset (saver_info *si, Bool lock_p);
#endif /* HAVE_XHPDISABLERESET */
#ifdef HAVE_XF86VMODE
# include <X11/extensions/xf86vmode.h>
static void xfree_lock_mode_switch (saver_info *si, Bool lock_p);
#endif /* HAVE_XF86VMODE */
#ifdef HAVE_XF86MISCSETGRABKEYSSTATE
# include <X11/extensions/xf86misc.h>
static void xfree_lock_grab_smasher (saver_info *si, Bool lock_p);
#endif /* HAVE_XF86MISCSETGRABKEYSSTATE */
#ifdef HAVE_RANDR
# include <X11/extensions/Xrandr.h>
#endif /* HAVE_RANDR */
#ifdef _VROOT_H_
ERROR! You must not include vroot.h in this file.
#endif
#ifdef HAVE_UNAME
# include <sys/utsname.h> /* for hostname info */
#endif /* HAVE_UNAME */
#include <ctype.h>
#ifndef VMS
# include <pwd.h>
#else /* VMS */
extern char *getenv(const char *name);
extern int validate_user(char *name, char *password);
static Bool
vms_passwd_valid_p(char *pw, Bool verbose_p)
{
return (validate_user (getenv("USER"), typed_passwd) == 1);
}
# undef passwd_valid_p
# define passwd_valid_p vms_passwd_valid_p
#endif /* VMS */
#define SAMPLE_INPUT "MMMMMMMMMMMM"
#undef MAX
#define MAX(a,b) ((a)>(b)?(a):(b))
typedef struct info_dialog_data info_dialog_data;
#define MAX_BYTES_PER_CHAR 8 /* UTF-8 uses no more than 3, I think */
#define MAX_PASSWD_CHARS 128 /* Longest possible passphrase */
struct passwd_dialog_data {
saver_screen_info *prompt_screen;
int previous_mouse_x, previous_mouse_y;
/* "Characters" in the password may be a variable number of bytes long.
typed_passwd contains the raw bytes.
typed_passwd_char_size indicates the size in bytes of each character,
so that we can make backspace work.
*/
char typed_passwd [MAX_PASSWD_CHARS * MAX_BYTES_PER_CHAR];
char typed_passwd_char_size [MAX_PASSWD_CHARS];
XtIntervalId timer;
int i_beam;
float ratio;
Position x, y;
Dimension width;
Dimension height;
Dimension border_width;
Bool echo_input;
Bool show_stars_p; /* "I regret that I have but one asterisk for my country."
-- Nathan Hale, 1776. */
char *heading_label;
char *body_label;
char *user_label;
mlstring *info_label;
/* The entry field shall only be displayed if prompt_label is not NULL */
mlstring *prompt_label;
char *date_label;
char *passwd_string;
Bool passwd_changed_p; /* Whether the user entry field needs redrawing */
Bool caps_p; /* Whether we saw a keypress with caps-lock on */
char *unlock_label;
char *login_label;
char *uname_label;
Bool show_uname_p;
XFontStruct *heading_font;
XFontStruct *body_font;
XFontStruct *label_font;
XFontStruct *passwd_font;
XFontStruct *date_font;
XFontStruct *button_font;
XFontStruct *uname_font;
Pixel foreground;
Pixel background;
Pixel border;
Pixel passwd_foreground;
Pixel passwd_background;
Pixel thermo_foreground;
Pixel thermo_background;
Pixel shadow_top;
Pixel shadow_bottom;
Pixel button_foreground;
Pixel button_background;
Dimension preferred_logo_width, logo_width;
Dimension preferred_logo_height, logo_height;
Dimension thermo_width;
Dimension internal_border;
Dimension shadow_width;
Dimension passwd_field_x, passwd_field_y;
Dimension passwd_field_width, passwd_field_height;
Dimension unlock_button_x, unlock_button_y;
Dimension unlock_button_width, unlock_button_height;
Dimension login_button_x, login_button_y;
Dimension login_button_width, login_button_height;
Dimension thermo_field_x, thermo_field_y;
Dimension thermo_field_height;
Pixmap logo_pixmap;
Pixmap logo_clipmask;
int logo_npixels;
unsigned long *logo_pixels;
Cursor passwd_cursor;
Bool unlock_button_down_p;
Bool login_button_down_p;
Bool login_button_p;
Bool login_button_enabled_p;
Bool button_state_changed_p; /* Refers to both buttons */
Pixmap save_under;
Pixmap user_entry_pixmap;
};
static void draw_passwd_window (saver_info *si);
static void update_passwd_window (saver_info *si, const char *printed_passwd,
float ratio);
static void destroy_passwd_window (saver_info *si);
static void undo_vp_motion (saver_info *si);
static void finished_typing_passwd (saver_info *si, passwd_dialog_data *pw);
static void cleanup_passwd_window (saver_info *si);
static void restore_background (saver_info *si);
extern void xss_authenticate(saver_info *si, Bool verbose_p);
static int
new_passwd_window (saver_info *si)
{
passwd_dialog_data *pw;
Screen *screen;
Colormap cmap;
saver_screen_info *ssi = &si->screens [mouse_screen (si)];
pw = (passwd_dialog_data *) calloc (1, sizeof(*pw));
if (!pw)
return -1;
/* Display the button only if the "newLoginCommand" pref is non-null.
*/
pw->login_button_p = (si->prefs.new_login_command &&
*si->prefs.new_login_command);
pw->passwd_cursor = XCreateFontCursor (si->dpy, XC_top_left_arrow);
pw->prompt_screen = ssi;
screen = pw->prompt_screen->screen;
cmap = DefaultColormapOfScreen (screen);
pw->show_stars_p = get_boolean_resource(si->dpy, "passwd.asterisks",
"Boolean");
pw->heading_label = get_string_resource (si->dpy, "passwd.heading.label",
"Dialog.Label.Label");
pw->body_label = get_string_resource (si->dpy, "passwd.body.label",
"Dialog.Label.Label");
pw->user_label = get_string_resource (si->dpy, "passwd.user.label",
"Dialog.Label.Label");
pw->unlock_label = get_string_resource (si->dpy, "passwd.unlock.label",
"Dialog.Button.Label");
pw->login_label = get_string_resource (si->dpy, "passwd.login.label",
"Dialog.Button.Label");
pw->date_label = get_string_resource (si->dpy, "dateFormat", "DateFormat");
if (!pw->heading_label)
pw->heading_label = strdup("ERROR: RESOURCES NOT INSTALLED CORRECTLY");
if (!pw->body_label)
pw->body_label = strdup("ERROR: RESOURCES NOT INSTALLED CORRECTLY");
if (!pw->user_label) pw->user_label = strdup("ERROR");
if (!pw->date_label) pw->date_label = strdup("ERROR");
if (!pw->unlock_label) pw->unlock_label = strdup("ERROR (UNLOCK)");
if (!pw->login_label) pw->login_label = strdup ("ERROR (LOGIN)") ;
/* Put the version number in the label. */
{
char *s = (char *) malloc (strlen(pw->heading_label) + 20);
sprintf(s, pw->heading_label, si->version);
free (pw->heading_label);
pw->heading_label = s;
}
/* Get hostname info */
pw->uname_label = strdup(""); /* Initialy, write nothing */
# ifdef HAVE_UNAME
{
struct utsname uts;
if (uname (&uts) == 0)
{
#if 0 /* Get the full hostname */
{
char *s;
if ((s = strchr(uts.nodename, '.')))
*s = 0;
}
#endif
char *s = strdup (uts.nodename);
free (pw->uname_label);
pw->uname_label = s;
}
}
# endif
pw->passwd_string = strdup("");
pw->heading_font =
splash_load_font (si->dpy, "passwd.headingFont", "Dialog.Font");
pw->button_font =
splash_load_font (si->dpy, "passwd.buttonFont", "Dialog.Font");
pw->body_font =
splash_load_font (si->dpy, "passwd.bodyFont", "Dialog.Font");
pw->label_font =
splash_load_font (si->dpy, "passwd.labelFont", "Dialog.Font");
pw->passwd_font =
splash_load_font (si->dpy, "passwd.passwdFont", "Dialog.Font");
pw->date_font =
splash_load_font (si->dpy, "passwd.dateFont", "Dialog.Font");
pw->uname_font =
splash_load_font (si->dpy, "passwd.unameFont", "Dialog.Font");
pw->show_uname_p = get_boolean_resource(si->dpy, "passwd.uname", "Boolean");
pw->foreground = get_pixel_resource (si->dpy, cmap,
"passwd.foreground",
"Dialog.Foreground" );
pw->background = get_pixel_resource (si->dpy, cmap,
"passwd.background",
"Dialog.Background" );
pw->border = get_pixel_resource (si->dpy, cmap,
"passwd.borderColor",
"Dialog.borderColor");
if (pw->foreground == pw->background)
{
/* Make sure the error messages show up. */
pw->foreground = BlackPixelOfScreen (screen);
pw->background = WhitePixelOfScreen (screen);
}
pw->passwd_foreground = get_pixel_resource (si->dpy, cmap,
"passwd.text.foreground",
"Dialog.Text.Foreground" );
pw->passwd_background = get_pixel_resource (si->dpy, cmap,
"passwd.text.background",
"Dialog.Text.Background" );
pw->button_foreground = get_pixel_resource (si->dpy, cmap,
"splash.Button.foreground",
"Dialog.Button.Foreground" );
pw->button_background = get_pixel_resource (si->dpy, cmap,
"splash.Button.background",
"Dialog.Button.Background" );
pw->thermo_foreground = get_pixel_resource (si->dpy, cmap,
"passwd.thermometer.foreground",
"Dialog.Thermometer.Foreground");
pw->thermo_background = get_pixel_resource ( si->dpy, cmap,
"passwd.thermometer.background",
"Dialog.Thermometer.Background");
pw->shadow_top = get_pixel_resource ( si->dpy, cmap,
"passwd.topShadowColor",
"Dialog.Foreground" );
pw->shadow_bottom = get_pixel_resource (si->dpy, cmap,
"passwd.bottomShadowColor",
"Dialog.Background" );
pw->preferred_logo_width = get_integer_resource (si->dpy,
"passwd.logo.width",
"Dialog.Logo.Width");
pw->preferred_logo_height = get_integer_resource (si->dpy,
"passwd.logo.height",
"Dialog.Logo.Height");
pw->thermo_width = get_integer_resource (si->dpy, "passwd.thermometer.width",
"Dialog.Thermometer.Width");
pw->internal_border = get_integer_resource (si->dpy,
"passwd.internalBorderWidth",
"Dialog.InternalBorderWidth");
pw->shadow_width = get_integer_resource (si->dpy, "passwd.shadowThickness",
"Dialog.ShadowThickness");
if (pw->preferred_logo_width == 0) pw->preferred_logo_width = 150;
if (pw->preferred_logo_height == 0) pw->preferred_logo_height = 150;
if (pw->internal_border == 0) pw->internal_border = 15;
if (pw->shadow_width == 0) pw->shadow_width = 4;
if (pw->thermo_width == 0) pw->thermo_width = pw->shadow_width;
/* We need to remember the mouse position and restore it afterward, or
sometimes (perhaps only with Xinerama?) the mouse gets warped to
inside the bounds of the lock dialog window.
*/
{
Window pointer_root, pointer_child;
int root_x, root_y, win_x, win_y;
unsigned int mask;
pw->previous_mouse_x = 0;
pw->previous_mouse_y = 0;
if (XQueryPointer (si->dpy, RootWindowOfScreen (pw->prompt_screen->screen),
&pointer_root, &pointer_child,
&root_x, &root_y, &win_x, &win_y, &mask))
{
pw->previous_mouse_x = root_x;
pw->previous_mouse_y = root_y;
if (si->prefs.verbose_p)
fprintf (stderr, "%s: %d: mouse is at %d,%d.\n",
blurb(), pw->prompt_screen->number,
pw->previous_mouse_x, pw->previous_mouse_y);
}
else if (si->prefs.verbose_p)
fprintf (stderr, "%s: %d: unable to determine mouse position?\n",
blurb(), pw->prompt_screen->number);
}
/* Before mapping the window, save a pixmap of the current screen.
When we lower the window, we restore these bits. This works,
because the running screenhack has already been sent SIGSTOP, so
we know nothing else is drawing right now! */
{
XGCValues gcv;
GC gc;
pw->save_under = XCreatePixmap (si->dpy,
pw->prompt_screen->screensaver_window,
pw->prompt_screen->width,
pw->prompt_screen->height,
pw->prompt_screen->current_depth);
gcv.function = GXcopy;
gc = XCreateGC (si->dpy, pw->save_under, GCFunction, &gcv);
XCopyArea (si->dpy, pw->prompt_screen->screensaver_window,
pw->save_under, gc,
0, 0,
pw->prompt_screen->width, pw->prompt_screen->height,
0, 0);
XFreeGC (si->dpy, gc);
}
si->pw_data = pw;
return 0;
}
Bool debug_passwd_window_p = False; /* used only by test-passwd.c */
/**
* info_msg and prompt may be NULL.
*/
static int
make_passwd_window (saver_info *si,
const char *info_msg,
const char *prompt,
Bool echo)
{
XSetWindowAttributes attrs;
unsigned long attrmask = 0;
passwd_dialog_data *pw;
Screen *screen;
Colormap cmap;
Dimension max_string_width_px;
saver_screen_info *ssi = &si->screens [mouse_screen (si)];
cleanup_passwd_window (si);
if (! ssi) /* WTF? Trying to prompt while no screens connected? */
return -1;
if (!si->pw_data)
if (new_passwd_window (si) < 0)
return -1;
if (!(pw = si->pw_data))
return -1;
pw->ratio = 1.0;
pw->prompt_screen = ssi;
if (si->prefs.verbose_p)
fprintf (stderr, "%s: %d: creating password dialog (\"%s\")\n",
blurb(), pw->prompt_screen->number,
info_msg ? info_msg : "");
screen = pw->prompt_screen->screen;
cmap = DefaultColormapOfScreen (screen);
pw->echo_input = echo;
max_string_width_px = ssi->width
- pw->shadow_width * 4
- pw->border_width * 2
- pw->thermo_width
- pw->preferred_logo_width
- pw->internal_border * 2;
/* As the string wraps it makes the window taller which makes the logo wider
* which leaves less room for the text which makes the string wrap. Uh-oh, a
* loop. By wrapping at a bit less than the available width, there's some
* room for the dialog to grow without going off the edge of the screen. */
max_string_width_px *= 0.75;
if (!info_msg && senesculent_p())
info_msg = ("\n"
"This version of XScreenSaver\n"
"is very old! Please upgrade!\n");
pw->info_label = mlstring_new(info_msg ? info_msg : pw->body_label,
pw->label_font, max_string_width_px);
{
int direction, ascent, descent;
XCharStruct overall;
pw->width = 0;
pw->height = 0;
/* Measure the heading_label. */
XTextExtents (pw->heading_font,
pw->heading_label, strlen(pw->heading_label),
&direction, &ascent, &descent, &overall);
if (overall.width > pw->width) pw->width = overall.width;
pw->height += ascent + descent;
/* Measure the uname_label. */
if ((strlen(pw->uname_label)) && pw->show_uname_p)
{
XTextExtents (pw->uname_font,
pw->uname_label, strlen(pw->uname_label),
&direction, &ascent, &descent, &overall);
if (overall.width > pw->width) pw->width = overall.width;
pw->height += ascent + descent;
}
{
Dimension w2 = 0, w3 = 0, button_w = 0;
Dimension h2 = 0, h3 = 0, button_h = 0;
const char *passwd_string = SAMPLE_INPUT;
/* Measure the user_label. */
XTextExtents (pw->label_font,
pw->user_label, strlen(pw->user_label),
&direction, &ascent, &descent, &overall);
if (overall.width > w2) w2 = overall.width;
h2 += ascent + descent;
/* Measure the info_label. */
if (pw->info_label->overall_width > pw->width)
pw->width = pw->info_label->overall_width;
h2 += pw->info_label->overall_height;
/* Measure the user string. */
XTextExtents (pw->passwd_font,
si->user, strlen(si->user),
&direction, &ascent, &descent, &overall);
overall.width += (pw->shadow_width * 4);
ascent += (pw->shadow_width * 4);
if (overall.width > w3) w3 = overall.width;
h3 += ascent + descent;
/* Measure the (dummy) passwd_string. */
if (prompt)
{
XTextExtents (pw->passwd_font,
passwd_string, strlen(passwd_string),
&direction, &ascent, &descent, &overall);
overall.width += (pw->shadow_width * 4);
ascent += (pw->shadow_width * 4);
if (overall.width > w3) w3 = overall.width;
h3 += ascent + descent;
/* Measure the prompt_label. */
max_string_width_px -= w3;
pw->prompt_label = mlstring_new (prompt, pw->label_font,
max_string_width_px);
if (pw->prompt_label->overall_width > w2)
w2 = pw->prompt_label->overall_width;
h2 += pw->prompt_label->overall_height;
w2 = w2 + w3 + (pw->shadow_width * 2);
h2 = MAX (h2, h3);
}
/* The "Unlock" button. */
XTextExtents (pw->label_font,
pw->unlock_label, strlen(pw->unlock_label),
&direction, &ascent, &descent, &overall);
button_w = overall.width;
button_h = ascent + descent;
/* Add some horizontal padding inside the button. */
button_w += ascent;
button_w += ((ascent + descent) / 2) + (pw->shadow_width * 2);
button_h += ((ascent + descent) / 2) + (pw->shadow_width * 2);
pw->unlock_button_width = button_w;
pw->unlock_button_height = button_h;
w2 = MAX (w2, button_w);
h2 += button_h * 1.5;
/* The "New Login" button */
pw->login_button_width = 0;
pw->login_button_height = 0;
if (pw->login_button_p)
{
pw->login_button_enabled_p = True;
/* Measure the "New Login" button */
XTextExtents (pw->button_font, pw->login_label,
strlen (pw->login_label),
&direction, &ascent, &descent, &overall);
button_w = overall.width;
button_h = ascent + descent;
/* Add some horizontal padding inside the buttons. */
button_w += ascent;
button_w += ((ascent + descent) / 2) + (pw->shadow_width * 2);
button_h += ((ascent + descent) / 2) + (pw->shadow_width * 2);
pw->login_button_width = button_w;
pw->login_button_height = button_h;
if (button_h > pw->unlock_button_height)
h2 += (button_h * 1.5 - pw->unlock_button_height * 1.5);
/* Use (2 * shadow_width) spacing between the buttons. Another
(2 * shadow_width) is required to account for button shadows. */
w2 = MAX (w2,
button_w + pw->unlock_button_width +
(pw->shadow_width * 4));
}
if (w2 > pw->width) pw->width = w2;
pw->height += h2;
}
pw->width += (pw->internal_border * 2);
pw->height += (pw->internal_border * 4);
pw->width += pw->thermo_width + (pw->shadow_width * 3);
if (pw->preferred_logo_height > pw->height)
pw->height = pw->logo_height = pw->preferred_logo_height;
else if (pw->height > pw->preferred_logo_height)
pw->logo_height = pw->height;
pw->logo_width = pw->logo_height;
pw->width += pw->logo_width;
}
attrmask |= CWOverrideRedirect; attrs.override_redirect = True;
if (debug_passwd_window_p)
attrs.override_redirect = False; /* kludge for test-passwd.c */
attrmask |= CWEventMask;
attrs.event_mask = (ExposureMask | KeyPressMask |
ButtonPressMask | ButtonReleaseMask);
/* Figure out where on the desktop to place the window so that it will
actually be visible; this takes into account virtual viewports as
well as Xinerama. */
{
saver_screen_info *ssi = &si->screens [mouse_screen (si)];
int x = ssi->x;
int y = ssi->y;
int w = ssi->width;
int h = ssi->height;
if (si->prefs.debug_p) w /= 2;
pw->x = x + ((w + pw->width) / 2) - pw->width;
pw->y = y + ((h + pw->height) / 2) - pw->height;
if (pw->x < x) pw->x = x;
if (pw->y < y) pw->y = y;
}
pw->border_width = get_integer_resource (si->dpy, "passwd.borderWidth",
"Dialog.BorderWidth");
/* Only create the window the first time around */
if (!si->passwd_dialog)
{
si->passwd_dialog =
XCreateWindow (si->dpy,
RootWindowOfScreen(screen),
pw->x, pw->y, pw->width, pw->height, pw->border_width,
DefaultDepthOfScreen (screen), InputOutput,
DefaultVisualOfScreen(screen),
attrmask, &attrs);
XSetWindowBackground (si->dpy, si->passwd_dialog, pw->background);
XSetWindowBorder (si->dpy, si->passwd_dialog, pw->border);
/* We use the default visual, not ssi->visual, so that the logo pixmap's
visual matches that of the si->passwd_dialog window. */
pw->logo_pixmap = xscreensaver_logo (ssi->screen,
/* ssi->current_visual, */
DefaultVisualOfScreen(screen),
si->passwd_dialog, cmap,
pw->background,
&pw->logo_pixels, &pw->logo_npixels,
&pw->logo_clipmask, True);
}
else /* On successive prompts, just resize the window */
{
XWindowChanges wc;
unsigned int mask = CWX | CWY | CWWidth | CWHeight;
wc.x = pw->x;
wc.y = pw->y;
wc.width = pw->width;
wc.height = pw->height;
XConfigureWindow (si->dpy, si->passwd_dialog, mask, &wc);
}
restore_background(si);
XMapRaised (si->dpy, si->passwd_dialog);
XSync (si->dpy, False);
move_mouse_grab (si, si->passwd_dialog,
pw->passwd_cursor,
pw->prompt_screen->number);
undo_vp_motion (si);
si->pw_data = pw;
if (cmap)
XInstallColormap (si->dpy, cmap);
draw_passwd_window (si);
return 0;
}
static void
draw_passwd_window (saver_info *si)
{
passwd_dialog_data *pw = si->pw_data;
XGCValues gcv;
GC gc1, gc2;
int spacing, height;
int x1, x2, x3, y1, y2;
int sw;
int tb_height;
/* Force redraw */
pw->passwd_changed_p = True;
pw->button_state_changed_p = True;
/* This height is the height of all the elements, not to be confused with
* the overall window height which is pw->height. It is used to compute
* the amount of spacing (padding) between elements. */
height = (pw->heading_font->ascent + pw->heading_font->descent +
pw->info_label->overall_height +
MAX (((pw->label_font->ascent + pw->label_font->descent) +
(pw->prompt_label ? pw->prompt_label->overall_height : 0)),
((pw->passwd_font->ascent + pw->passwd_font->descent) +
(pw->shadow_width * 2)) * (pw->prompt_label ? 2 : 1)) +
pw->date_font->ascent + pw->date_font->descent);
if ((strlen(pw->uname_label)) && pw->show_uname_p)
height += (pw->uname_font->ascent + pw->uname_font->descent);
height += ((pw->button_font->ascent + pw->button_font->descent) * 2 +
2 * pw->shadow_width);
spacing = ((pw->height - 2 * pw->shadow_width
- pw->internal_border - height)
/ 10);
if (spacing < 0) spacing = 0;
gcv.foreground = pw->foreground;
gc1 = XCreateGC (si->dpy, si->passwd_dialog, GCForeground, &gcv);
gc2 = XCreateGC (si->dpy, si->passwd_dialog, GCForeground, &gcv);
x1 = pw->logo_width + pw->thermo_width + (pw->shadow_width * 3);
x3 = pw->width - (pw->shadow_width * 2);
y1 = (pw->shadow_width * 2) + spacing + spacing;
/* top heading
*/
XSetFont (si->dpy, gc1, pw->heading_font->fid);
sw = string_width (pw->heading_font, pw->heading_label);
x2 = (x1 + ((x3 - x1 - sw) / 2));
y1 += spacing + pw->heading_font->ascent + pw->heading_font->descent;
XDrawString (si->dpy, si->passwd_dialog, gc1, x2, y1,
pw->heading_label, strlen(pw->heading_label));
/* uname below top heading
*/
if ((strlen(pw->uname_label)) && pw->show_uname_p)
{
XSetFont (si->dpy, gc1, pw->uname_font->fid);
y1 += spacing + pw->uname_font->ascent + pw->uname_font->descent;
sw = string_width (pw->uname_font, pw->uname_label);
x2 = (x1 + ((x3 - x1 - sw) / 2));
XDrawString (si->dpy, si->passwd_dialog, gc1, x2, y1,
pw->uname_label, strlen(pw->uname_label));
}
/* the info_label (below uname)
*/
x2 = (x1 + ((x3 - x1 - pw->info_label->overall_width) / 2));
y1 += spacing + pw->info_label->font_height / 2;
mlstring_draw(si->dpy, si->passwd_dialog, gc1, pw->info_label,
x2, y1);
y1 += pw->info_label->overall_height;
tb_height = (pw->passwd_font->ascent + pw->passwd_font->descent +
(pw->shadow_width * 4));
/* the "User:" prompt
*/
y2 = y1;
XSetForeground (si->dpy, gc1, pw->foreground);
XSetFont (si->dpy, gc1, pw->label_font->fid);
y1 += (spacing + tb_height + pw->shadow_width);
x2 = (x1 + pw->internal_border +
MAX(string_width (pw->label_font, pw->user_label),
pw->prompt_label ? pw->prompt_label->overall_width : 0));
XDrawString (si->dpy, si->passwd_dialog, gc1,
x2 - string_width (pw->label_font, pw->user_label),
y1 - pw->passwd_font->descent,
pw->user_label, strlen(pw->user_label));
/* the prompt_label prompt
*/
if (pw->prompt_label)
{
y1 += tb_height - pw->label_font->ascent + pw->shadow_width;
mlstring_draw(si->dpy, si->passwd_dialog, gc1, pw->prompt_label,
x2 - pw->prompt_label->overall_width, y1);
}
/* the "user name" text field
*/
y1 = y2;
XSetForeground (si->dpy, gc1, pw->passwd_foreground);
XSetForeground (si->dpy, gc2, pw->passwd_background);
XSetFont (si->dpy, gc1, pw->passwd_font->fid);
y1 += (spacing + tb_height);
x2 += (pw->shadow_width * 4);
pw->passwd_field_width = x3 - x2 - pw->internal_border;
pw->passwd_field_height = (pw->passwd_font->ascent +
pw->passwd_font->descent +
pw->shadow_width);
XFillRectangle (si->dpy, si->passwd_dialog, gc2,
x2 - pw->shadow_width,
y1 - (pw->passwd_font->ascent + pw->passwd_font->descent),
pw->passwd_field_width, pw->passwd_field_height);
XDrawString (si->dpy, si->passwd_dialog, gc1,
x2,
y1 - pw->passwd_font->descent,
si->user, strlen(si->user));
/* the password/prompt text field
*/
if (pw->prompt_label)
{
y1 += (spacing + pw->prompt_label->overall_height + pw->shadow_width*2);
pw->passwd_field_x = x2 - pw->shadow_width;
pw->passwd_field_y = y1 - (pw->passwd_font->ascent +
pw->passwd_font->descent);
}
/* The shadow around the text fields
*/
y1 = y2;
y1 += (spacing + (pw->shadow_width * 3));
x1 = x2 - (pw->shadow_width * 2);
x2 = pw->passwd_field_width + (pw->shadow_width * 2);
y2 = pw->passwd_field_height + (pw->shadow_width * 2);
draw_shaded_rectangle (si->dpy, si->passwd_dialog,
x1, y1, x2, y2,
pw->shadow_width,
pw->shadow_bottom, pw->shadow_top);
if (pw->prompt_label)
{
y1 += (spacing + pw->prompt_label->overall_height + pw->shadow_width*2);
draw_shaded_rectangle (si->dpy, si->passwd_dialog,
x1, y1, x2, y2,
pw->shadow_width,
pw->shadow_bottom, pw->shadow_top);
}
/* The date, below the text fields
*/
{
char buf[100];
time_t now = time ((time_t *) 0);
struct tm *tm = localtime (&now);
memset (buf, 0, sizeof(buf));
strftime (buf, sizeof(buf)-1, pw->date_label, tm);
XSetForeground (si->dpy, gc1, pw->foreground);
XSetFont (si->dpy, gc1, pw->date_font->fid);
y1 += pw->shadow_width;
y1 += (spacing + tb_height);
y1 += spacing/2;
sw = string_width (pw->date_font, buf);
x2 = x1 + x2 - sw;
XDrawString (si->dpy, si->passwd_dialog, gc1, x2, y1, buf, strlen(buf));
}
/* Set up the GCs for the "New Login" and "Unlock" buttons.
*/
XSetForeground(si->dpy, gc1, pw->button_foreground);
XSetForeground(si->dpy, gc2, pw->button_background);
XSetFont(si->dpy, gc1, pw->button_font->fid);
/* The "Unlock" button */
x2 = pw->width - pw->internal_border - (pw->shadow_width * 2);
/* right aligned button */
x1 = x2 - pw->unlock_button_width;
/* Add half the difference between y1 and the internal edge.
* It actually looks better if the internal border is ignored. */
y1 += ((pw->height - MAX (pw->unlock_button_height, pw->login_button_height)
- spacing - y1)
/ 2);
pw->unlock_button_x = x1;
pw->unlock_button_y = y1;
/* The "New Login" button
*/
if (pw->login_button_p)
{
/* Using the same GC as for the Unlock button */
sw = string_width (pw->button_font, pw->login_label);
/* left aligned button */
x1 = (pw->logo_width + pw->thermo_width + (pw->shadow_width * 3) +
pw->internal_border);
pw->login_button_x = x1;
pw->login_button_y = y1;
}
/* The logo
*/
x1 = pw->shadow_width * 6;
y1 = pw->shadow_width * 6;
x2 = pw->logo_width - (pw->shadow_width * 12);
y2 = pw->logo_height - (pw->shadow_width * 12);
if (pw->logo_pixmap)
{
Window root;
int x, y;
unsigned int w, h, bw, d;
XGetGeometry (si->dpy, pw->logo_pixmap, &root, &x, &y, &w, &h, &bw, &d);
XSetForeground (si->dpy, gc1, pw->foreground);
XSetBackground (si->dpy, gc1, pw->background);
XSetClipMask (si->dpy, gc1, pw->logo_clipmask);
XSetClipOrigin (si->dpy, gc1,
x1 + ((x2 - (int)w) / 2),
y1 + ((y2 - (int)h) / 2));
if (d == 1)
XCopyPlane (si->dpy, pw->logo_pixmap, si->passwd_dialog, gc1,
0, 0, w, h,
x1 + ((x2 - (int)w) / 2),
y1 + ((y2 - (int)h) / 2),
1);
else
XCopyArea (si->dpy, pw->logo_pixmap, si->passwd_dialog, gc1,
0, 0, w, h,
x1 + ((x2 - (int)w) / 2),
y1 + ((y2 - (int)h) / 2));
}
/* The thermometer
*/
XSetForeground (si->dpy, gc1, pw->thermo_foreground);
XSetForeground (si->dpy, gc2, pw->thermo_background);
pw->thermo_field_x = pw->logo_width + pw->shadow_width;
pw->thermo_field_y = pw->shadow_width * 5;
pw->thermo_field_height = pw->height - (pw->shadow_width * 10);
#if 0
/* Solid border inside the logo box. */
XSetForeground (si->dpy, gc1, pw->foreground);
XDrawRectangle (si->dpy, si->passwd_dialog, gc1, x1, y1, x2-1, y2-1);
#endif
/* The shadow around the logo
*/
draw_shaded_rectangle (si->dpy, si->passwd_dialog,
pw->shadow_width * 4,
pw->shadow_width * 4,
pw->logo_width - (pw->shadow_width * 8),
pw->logo_height - (pw->shadow_width * 8),
pw->shadow_width,
pw->shadow_bottom, pw->shadow_top);
/* The shadow around the thermometer
*/
draw_shaded_rectangle (si->dpy, si->passwd_dialog,
pw->logo_width,
pw->shadow_width * 4,
pw->thermo_width + (pw->shadow_width * 2),
pw->height - (pw->shadow_width * 8),
pw->shadow_width,
pw->shadow_bottom, pw->shadow_top);
#if 1
/* Solid border inside the thermometer. */
XSetForeground (si->dpy, gc1, pw->foreground);
XDrawRectangle (si->dpy, si->passwd_dialog, gc1,
pw->thermo_field_x, pw->thermo_field_y,
pw->thermo_width - 1, pw->thermo_field_height - 1);
#endif
/* The shadow around the whole window
*/
draw_shaded_rectangle (si->dpy, si->passwd_dialog,
0, 0, pw->width, pw->height, pw->shadow_width,