1 /* XDigger  Copyright (C) 1988-99 Alexander Lang.
2 
3 XDigger is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7 
8 XDigger is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License
14 along with this program; see the file COPYING.  If not, write to
15 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
16 
17 #include <X11/Xlib.h>
18 #include <X11/Xutil.h>
19 #include <X11/keysym.h>
20 
21 #include <stdio.h>
22 #include <string.h>
23 #include <pwd.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 
27 #include "configure.h"
28 #include "drawpixmaps.h"
29 #include "drawtext.h"
30 #include "icon.h"
31 #include "runlevels.h"
32 #include "xdigger.h"
33 
34 #define NAMELENGH 20
35 
36 typedef struct
37 {
38   int score;
39   char name[NAMELENGH + 1];
40 } ScoreEntry;
41 
42 ScoreEntry highscore[20];
43 
LoadHighScore()44 void LoadHighScore()
45 {
46   char filename[256];
47   FILE *filehandle;
48   int i, n = 0;
49 
50   for (i=0; i<NAMELENGH; i++)
51     {
52       highscore[i].score = 0;
53       strcpy(highscore[i].name, "");
54     }
55 
56   strcat(strcpy(filename, XDIGGER_HISCORE_DIR), "/xdigger.hiscore");
57   if ((filehandle = fopen(filename, "r")) == NULL)
58     {
59       XBell(display, -50);
60       fprintf(stderr, "%s: can't read %s\n", progname, filename);
61       strcpy(filename, progname); strcat(filename, ".hiscore");
62       fprintf(stderr, "%s: try %s ... ", progname, filename);
63       if ((filehandle = fopen(filename, "r")) == NULL)
64 /* 	  fprintf(stderr, "can't read %s\n", filename); */
65 	fprintf(stderr, "failed.\n");
66       else
67 	fprintf(stderr, "ok.\n");
68     }
69   if (filehandle != NULL)
70     {
71       n = fread(highscore, sizeof(highscore), 1, filehandle);
72       fclose(filehandle);
73     }
74   if (n < 1) /*sizeof(highscore)*/
75     {
76       highscore[0].score = 10000; strcpy(highscore[0].name, "--------------");
77       highscore[1].score =  9000; strcpy(highscore[1].name, "XDigger");
78       highscore[2].score =  8000; strcpy(highscore[2].name, "(c) 1988-99 by");
79       highscore[3].score =  7000; strcpy(highscore[3].name, "Alexander Lang");
80       highscore[4].score =  6000; strcpy(highscore[4].name, "--------------");
81     }
82 } /* LoadHighScore() */
83 
SaveHighScore()84 void SaveHighScore()
85 {
86   char filename[256];
87   FILE *filehandle;
88   int n = 0;
89 
90   strcat(strcpy(filename, XDIGGER_HISCORE_DIR), "/xdigger.hiscore");
91   if ((filehandle = fopen(filename, "w")) == NULL)
92     {
93       XBell(display, -50);
94       fprintf(stderr, "%s: can't write %s\n", progname, filename);
95       strcpy(filename, progname); strcat(filename, ".hiscore");
96       fprintf(stderr, "try %s ... ", filename);
97       if ((filehandle = fopen(filename, "w")) == NULL)
98 /* 	fprintf(stderr, "can't write %s\n", filename); */
99 	fprintf(stderr, "failed.\n");
100       else
101 	fprintf(stderr, "ok.\n");
102     }
103   if (filehandle != NULL)
104     {
105       n = fwrite(highscore, sizeof(highscore), 1, filehandle);
106       fclose(filehandle);
107     }
108   if (n < 1) /*sizeof(highscore)*/
109     fprintf(stderr, "%s: an error occured while writing highscorefile\n",
110             progname);
111 } /* SaveHighScore() */
112 
113 /* char *GetUserName() */
114 /* { */
115 /*   struct passwd *who; */
116 /*   char name[257], *c; */
117 
118 /*   who = getpwuid(getuid()); */
119 /*   strncpy(name, who->pw_gecos, 256); */
120 /*   c = strchr(name, ',') ; */
121 /*   if (c != NULL) *c = '\0'; */
122 /*   return(name); */
123 /* } char *GetUserName() */
124 
GetUserName(char * dest,size_t n)125 void GetUserName(char *dest, size_t n)
126 {
127   struct passwd *who;
128   char name[257], *c;
129 
130   who = getpwuid(getuid());
131   strncpy(name, who->pw_gecos, 256);
132   c = strchr(name, ',') ;
133   if (c != NULL) *c = '\0';
134   strncpy(dest, name, n);
135 } /* GetUserName(char *dest, size_t n) */
136 
InsertScore(int score,char * name)137 int InsertScore(int score, char *name)
138 {
139   int i, j, erg;
140 
141   erg = -1;
142   if (!ones_cheated) for (i=0; i<20; i++)
143     if (score > highscore[i].score)
144     {
145       erg = i;
146       for (j=19; j>i; j--)
147 	{
148 	  highscore[j].score = highscore[j-1].score;
149 	  strcpy(highscore[j].name, highscore[j-1].name);
150 	}
151       highscore[i].score = score;
152       strncpy(highscore[i].name, name, NAMELENGH);
153       break;
154     }
155   return(erg);
156 } /* int InsertScore(int score, char *name) */
157 
InitHighScoreText()158 void InitHighScoreText()
159 {
160   int i;
161   char entry[NAMELENGH + 10];
162 
163   WriteTextStr("...well done, please enter your name :", 1, 2,
164 	       kcf_gelb, kcb_tuerkis);
165   WriteTextStr("HIGHSCORE :", 8, 4, kcf_gelb, kcb_tuerkis);
166   WriteTextStr("\217\217\217\217\217\217\217\217\217\217\217", 8, 5,
167 	       kcf_gelb, kcb_tuerkis);
168 
169   for (i=0; i<20; i++)
170     {
171       sprintf(entry, "%.6d  %s", highscore[i].score, highscore[i].name);
172       WriteTextStr(entry, 10, 7+i, kcf_gelb, kcb_tuerkis);
173     }
174 } /* InitHighScoreText() */
175 
HighScore(int score)176 void HighScore(int score)
177 {
178   XSetWindowAttributes setwindowattributes;
179   XWindowAttributes windowattributes;
180   unsigned long valuemask;
181   XEvent event;
182   Bool Done = False;
183   KeySym keysym;
184   char buffer[20];
185   int bufsize = 20, count;
186   char name[NAMELENGH + 1], *nameinput;
187   int inpy, y;
188 /*   XComposeStatus cs; */
189 
190   strcpy(LastArgv, " (HiScore)");
191   setwindowattributes.background_pixel = kcb_tuerkis;
192   valuemask = CWBackPixel;
193   XChangeWindowAttributes(display, mainwindow, valuemask, &setwindowattributes);
194 
195   LoadHighScore();
196   GetUserName(name, 20);
197   y = InsertScore(score, name);
198   nameinput = highscore[y].name;
199   inpy = y + 7;
200   ClearCharField(kcb_tuerkis, True);
201   InitHighScoreText();
202   if (y>=0)
203     WriteTextStr("\177", 18 + strlen(nameinput), inpy, kcf_gelb, kcb_tuerkis);
204   DrawTextField();
205 
206   while (!Done)
207     {
208       if (!XEventsQueued(display, QueuedAfterReading))
209 	{
210 	  XDigger_Idle();
211 	  XFlush(display);
212 	} /* if (!XEventsQueued()) */
213       else
214 	{
215 	  XNextEvent(display, &event);
216           if (event.xany.window == iconwindow)
217             Handle_IconWindow_Event(&event); else
218 	  switch(event.type)
219 	    {
220 	    case Expose:
221               /* Resize ? */
222               XGetWindowAttributes(display, mainwindow, &windowattributes);
223               if (xpmmag != windowattributes.width / 320)
224                 ResizeXPixmaps(windowattributes.width / 320);
225 
226 	      Mark_CharField(&event.xexpose, 30+2*vertvar);
227 	      if (event.xexpose.count == 0)
228 		DrawTextField();
229 	      break;
230 	    case MappingNotify:
231 	      XRefreshKeyboardMapping(&event.xmapping);
232 	      break;
233 	    case KeyPress:
234 	      count = XLookupString(&event.xkey, buffer, bufsize, &keysym,
235 				    NULL);
236 	      buffer[count] = '\0';
237 /* 	      keysym = XKeycodeToKeysym(display, event.xkey.keycode, 0); */
238 	      if ((strlen(nameinput) < 20) && (strlen(buffer) == 1) &&
239 		  (0x20 <= buffer[0]) && (y>=0))
240 		{
241 		  strcat(nameinput, buffer);
242 		  WriteTextStr(nameinput, 18, inpy, kcf_gelb, kcb_tuerkis);
243 		  WriteTextStr("\177", 18 + strlen(nameinput), inpy,
244 			       kcf_gelb, kcb_tuerkis);
245 		  DrawTextField();
246 		}
247 	      switch (keysym)
248 		{
249 		case XK_BackSpace:
250 		  if ((strlen(nameinput) > 0) && (y>=0))
251 		    {
252 		      nameinput[strlen(nameinput) - 1] = '\0';
253 		      WriteTextStr(nameinput, 18, inpy, kcf_gelb, kcb_tuerkis);
254 		      WriteTextStr("\177 ", 18 + strlen(nameinput), inpy,
255 				   kcf_gelb, kcb_tuerkis);
256 		      DrawTextField();
257 		    }
258 		  break;
259 		case XK_Return:
260 		  Done = True;
261 		  break;
262 		case XK_Escape:
263 		  Done = True;
264 		  break;
265 		}
266 	      break;
267 	    case ClientMessage:
268 	      if (event.xclient.data.l[0] == protocol[0])
269 		{
270 		  /*exit(0);*/
271 		  quit = True;
272 		  Done = True;
273 		  break;
274 		}
275 	      break ;
276 	    } /* switch(event.type) */
277 	} /* if (!XEventsQueued()) else */
278     } /* while (!Done) */
279   SaveHighScore();
280   strcpy(LastArgv, "");
281 } /* HighScore(int score) */
282