Commit | Line | Data |
---|---|---|
6e44da43 | 1 | ;;; gamegrid.el --- library for implementing grid-based games on Emacs |
3d4ae13e | 2 | |
f2e3589a | 3 | ;; Copyright (C) 1997, 1998, 2001, 2002, 2003, 2004, |
2f043267 | 4 | ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
3d4ae13e RS |
5 | |
6 | ;; Author: Glynn Clements <glynn@sensei.co.uk> | |
7 | ;; Version: 1.02 | |
8 | ;; Created: 1997-08-13 | |
9 | ;; Keywords: games | |
10 | ||
11 | ;; This file is part of GNU Emacs. | |
12 | ||
b1fc2b50 | 13 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
3d4ae13e | 14 | ;; it under the terms of the GNU General Public License as published by |
b1fc2b50 GM |
15 | ;; the Free Software Foundation, either version 3 of the License, or |
16 | ;; (at your option) any later version. | |
3d4ae13e RS |
17 | |
18 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
19 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | ;; GNU General Public License for more details. | |
22 | ||
23 | ;; You should have received a copy of the GNU General Public License | |
b1fc2b50 | 24 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
3d4ae13e RS |
25 | |
26 | ;;; Commentary: | |
27 | ||
6e44da43 PJ |
28 | ;;; Code: |
29 | ||
3d4ae13e RS |
30 | (eval-when-compile |
31 | (require 'cl)) | |
32 | ||
33 | ;; ;;;;;;;;;;;;; buffer-local variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
34 | ||
35 | (defvar gamegrid-use-glyphs t | |
36 | "Non-nil means use glyphs when available.") | |
37 | ||
38 | (defvar gamegrid-use-color t | |
39 | "Non-nil means use color when available.") | |
40 | ||
41 | (defvar gamegrid-font "-*-courier-medium-r-*-*-*-140-100-75-*-*-iso8859-*" | |
42 | "Name of the font used in X mode.") | |
43 | ||
0f7df8ac RS |
44 | (defvar gamegrid-face nil |
45 | "Indicates the face to use as a default.") | |
46 | (make-variable-buffer-local 'gamegrid-face) | |
47 | ||
3d4ae13e RS |
48 | (defvar gamegrid-display-options nil) |
49 | ||
50 | (defvar gamegrid-buffer-width 0) | |
51 | (defvar gamegrid-buffer-height 0) | |
52 | (defvar gamegrid-blank 0) | |
53 | ||
54 | (defvar gamegrid-timer nil) | |
55 | ||
56 | (defvar gamegrid-display-mode nil) | |
57 | ||
58 | (defvar gamegrid-display-table) | |
59 | ||
60 | (defvar gamegrid-face-table nil) | |
61 | ||
62 | (defvar gamegrid-buffer-start 1) | |
63 | ||
64 | (defvar gamegrid-score-file-length 50 | |
65 | "Number of high scores to keep") | |
66 | ||
23afd1c8 CY |
67 | (defvar gamegrid-user-score-file-directory |
68 | (concat user-emacs-directory "games") | |
3d43e48a CW |
69 | "A directory for game scores which can't be shared. |
70 | If Emacs was built without support for shared game scores, then this | |
71 | directory will be used.") | |
72 | ||
3d4ae13e RS |
73 | (make-variable-buffer-local 'gamegrid-use-glyphs) |
74 | (make-variable-buffer-local 'gamegrid-use-color) | |
75 | (make-variable-buffer-local 'gamegrid-font) | |
76 | (make-variable-buffer-local 'gamegrid-display-options) | |
77 | (make-variable-buffer-local 'gamegrid-buffer-width) | |
78 | (make-variable-buffer-local 'gamegrid-buffer-height) | |
79 | (make-variable-buffer-local 'gamegrid-blank) | |
80 | (make-variable-buffer-local 'gamegrid-timer) | |
81 | (make-variable-buffer-local 'gamegrid-display-mode) | |
82 | (make-variable-buffer-local 'gamegrid-display-table) | |
83 | (make-variable-buffer-local 'gamegrid-face-table) | |
84 | (make-variable-buffer-local 'gamegrid-buffer-start) | |
85 | (make-variable-buffer-local 'gamegrid-score-file-length) | |
86 | ||
87 | ;; ;;;;;;;;;;;;; global variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
88 | ||
89 | (defvar gamegrid-grid-x-face nil) | |
90 | (defvar gamegrid-mono-x-face nil) | |
91 | (defvar gamegrid-mono-tty-face nil) | |
92 | ||
93 | ;; ;;;;;;;;;;;;; constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
94 | ||
95 | (defconst gamegrid-glyph-height 16) | |
96 | ||
97 | (defconst gamegrid-xpm "\ | |
98 | /* XPM */ | |
99 | static char *noname[] = { | |
100 | /* width height ncolors chars_per_pixel */ | |
101 | \"16 16 3 1\", | |
102 | /* colors */ | |
103 | \"+ s col1\", | |
104 | \". s col2\", | |
105 | \"- s col3\", | |
106 | /* pixels */ | |
107 | \"---------------+\", | |
108 | \"--------------++\", | |
109 | \"--............++\", | |
110 | \"--............++\", | |
111 | \"--............++\", | |
112 | \"--............++\", | |
113 | \"--............++\", | |
114 | \"--............++\", | |
115 | \"--............++\", | |
116 | \"--............++\", | |
117 | \"--............++\", | |
118 | \"--............++\", | |
119 | \"--............++\", | |
120 | \"--............++\", | |
121 | \"-+++++++++++++++\", | |
122 | \"++++++++++++++++\" | |
123 | }; | |
124 | " | |
125 | "XPM format image used for each square") | |
126 | ||
0f7df8ac RS |
127 | (defvar gamegrid-xbm "\ |
128 | /* gamegrid XBM */ | |
129 | #define gamegrid_width 16 | |
130 | #define gamegrid_height 16 | |
131 | static unsigned char gamegrid_bits[] = { | |
132 | 0xff, 0xff, 0xff, 0x7f, 0xff, 0x3f, 0xaf, 0x0a, 0x57, 0x15, 0xaf, 0x0a, | |
133 | 0x57, 0x15, 0xaf, 0x0a, 0x57, 0x15, 0xaf, 0x0a, 0x57, 0x15, 0xaf, 0x0a, | |
134 | 0x57, 0x15, 0x07, 0x00, 0x03, 0x00, 0x01, 0x00 };" | |
135 | "XBM format image used for each square.") | |
136 | ||
3d4ae13e RS |
137 | ;; ;;;;;;;;;;;;;;;; miscellaneous functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
138 | ||
139 | (defsubst gamegrid-characterp (arg) | |
140 | (if (fboundp 'characterp) | |
141 | (characterp arg) | |
142 | (integerp arg))) | |
143 | ||
144 | (defsubst gamegrid-event-x (event) | |
145 | (if (fboundp 'event-x) | |
146 | (event-x event) | |
147 | (car (posn-col-row (event-end event))))) | |
148 | ||
149 | (defsubst gamegrid-event-y (event) | |
150 | (if (fboundp 'event-y) | |
151 | (event-y event) | |
152 | (cdr (posn-col-row (event-end event))))) | |
153 | ||
154 | ;; ;;;;;;;;;;;;; display functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
155 | ||
156 | (defun gamegrid-color (color shade) | |
157 | (let* ((v (floor (* shade 255))) | |
158 | (r (* v (aref color 0))) | |
159 | (g (* v (aref color 1))) | |
160 | (b (* v (aref color 2)))) | |
161 | (format "#%02x%02x%02x" r g b))) | |
162 | ||
163 | (defun gamegrid-set-font (face) | |
164 | (if gamegrid-font | |
165 | (condition-case nil | |
166 | (set-face-font face gamegrid-font) | |
279b254c | 167 | (error nil)))) |
3d4ae13e RS |
168 | |
169 | (defun gamegrid-setup-face (face color) | |
170 | (set-face-foreground face color) | |
171 | (set-face-background face color) | |
172 | (gamegrid-set-font face) | |
173 | (condition-case nil | |
174 | (set-face-background-pixmap face [nothing]);; XEmacs | |
279b254c | 175 | (error nil)) |
3d4ae13e RS |
176 | (condition-case nil |
177 | (set-face-background-pixmap face nil);; Emacs | |
279b254c | 178 | (error nil))) |
3d4ae13e RS |
179 | |
180 | (defun gamegrid-make-mono-tty-face () | |
181 | (let ((face (make-face 'gamegrid-mono-tty-face))) | |
bf7f684c | 182 | (set-face-inverse-video-p face t) |
3d4ae13e RS |
183 | face)) |
184 | ||
185 | (defun gamegrid-make-color-tty-face (color) | |
a5bce90d | 186 | (let* ((color-str (if (symbolp color) (symbol-value color) color)) |
c7e6cdba | 187 | (name (intern (format "gamegrid-color-tty-face-%s" color-str))) |
3d4ae13e | 188 | (face (make-face name))) |
c7e6cdba | 189 | (gamegrid-setup-face face color-str) |
3d4ae13e RS |
190 | face)) |
191 | ||
192 | (defun gamegrid-make-grid-x-face () | |
193 | (let ((face (make-face 'gamegrid-x-border-face))) | |
194 | (gamegrid-set-font face) | |
195 | face)) | |
196 | ||
197 | (defun gamegrid-make-mono-x-face () | |
198 | (let ((face (make-face 'gamegrid-mono-x-face)) | |
199 | (color (face-foreground 'default))) | |
200 | (if (null color) | |
201 | (setq color | |
202 | (cdr-safe (assq 'foreground-color (frame-parameters))))) | |
203 | (gamegrid-setup-face face color) | |
204 | face)) | |
205 | ||
206 | (defun gamegrid-make-color-x-face (color) | |
207 | (let* ((hex (gamegrid-color color 1.0)) | |
208 | (name (intern (format "gamegrid-color-x-face-%s" hex))) | |
209 | (face (make-face name))) | |
c7e6cdba | 210 | (gamegrid-setup-face face hex) |
3d4ae13e RS |
211 | face)) |
212 | ||
213 | (defun gamegrid-make-face (data-spec-list color-spec-list) | |
214 | (let ((data (gamegrid-match-spec-list data-spec-list)) | |
215 | (color (gamegrid-match-spec-list color-spec-list))) | |
216 | (case data | |
217 | ('color-x | |
218 | (gamegrid-make-color-x-face color)) | |
219 | ('grid-x | |
220 | (unless gamegrid-grid-x-face | |
221 | (setq gamegrid-grid-x-face (gamegrid-make-grid-x-face))) | |
222 | gamegrid-grid-x-face) | |
223 | ('mono-x | |
224 | (unless gamegrid-mono-x-face | |
225 | (setq gamegrid-mono-x-face (gamegrid-make-mono-x-face))) | |
226 | gamegrid-mono-x-face) | |
227 | ('color-tty | |
228 | (gamegrid-make-color-tty-face color)) | |
229 | ('mono-tty | |
230 | (unless gamegrid-mono-tty-face | |
231 | (setq gamegrid-mono-tty-face (gamegrid-make-mono-tty-face))) | |
232 | gamegrid-mono-tty-face)))) | |
233 | ||
234 | (defun gamegrid-colorize-glyph (color) | |
0f7df8ac RS |
235 | (find-image `((:type xpm :data ,gamegrid-xpm |
236 | :ascent center | |
a1506d29 | 237 | :color-symbols |
0f7df8ac RS |
238 | (("col1" . ,(gamegrid-color color 0.6)) |
239 | ("col2" . ,(gamegrid-color color 0.8)) | |
240 | ("col3" . ,(gamegrid-color color 1.0)))) | |
241 | (:type xbm :data ,gamegrid-xbm | |
242 | :ascent center | |
243 | :foreground ,(gamegrid-color color 1.0) | |
244 | :background ,(gamegrid-color color 0.5))))) | |
3d4ae13e RS |
245 | |
246 | (defun gamegrid-match-spec (spec) | |
247 | (let ((locale (car spec)) | |
248 | (value (cadr spec))) | |
249 | (and (or (eq locale t) | |
250 | (and (listp locale) | |
251 | (memq gamegrid-display-mode locale)) | |
252 | (and (symbolp locale) | |
253 | (eq gamegrid-display-mode locale))) | |
254 | value))) | |
255 | ||
256 | (defun gamegrid-match-spec-list (spec-list) | |
257 | (and spec-list | |
258 | (or (gamegrid-match-spec (car spec-list)) | |
259 | (gamegrid-match-spec-list (cdr spec-list))))) | |
260 | ||
261 | (defun gamegrid-make-glyph (data-spec-list color-spec-list) | |
262 | (let ((data (gamegrid-match-spec-list data-spec-list)) | |
263 | (color (gamegrid-match-spec-list color-spec-list))) | |
264 | (cond ((gamegrid-characterp data) | |
265 | (vector data)) | |
266 | ((eq data 'colorize) | |
267 | (gamegrid-colorize-glyph color)) | |
0f7df8ac RS |
268 | ((listp data) |
269 | (find-image data)) ;untested! | |
3d4ae13e | 270 | ((vectorp data) |
0f7df8ac | 271 | (gamegrid-make-image-from-vector data))))) |
3d4ae13e | 272 | |
0f7df8ac RS |
273 | (defun gamegrid-make-image-from-vector (vect) |
274 | "Convert an XEmacs style \"glyph\" to an image-spec." | |
275 | (let ((l (list 'image :type))) | |
276 | (dotimes (n (length vect)) | |
277 | (setf l (nconc l (list (aref vect n))))) | |
278 | (nconc l (list :ascent 'center)))) | |
3d4ae13e RS |
279 | |
280 | (defun gamegrid-display-type () | |
0f7df8ac RS |
281 | (cond ((and gamegrid-use-glyphs |
282 | (display-images-p)) | |
283 | 'glyph) | |
284 | ((and gamegrid-use-color | |
285 | (display-graphic-p) | |
286 | (display-color-p)) | |
287 | 'color-x) | |
288 | ((display-graphic-p) | |
289 | 'mono-x) | |
290 | ((and gamegrid-use-color | |
291 | (display-color-p)) | |
292 | 'color-tty) | |
293 | ((display-multi-font-p) ;??? | |
294 | 'mono-tty) | |
295 | (t | |
296 | 'emacs-tty))) | |
3d4ae13e RS |
297 | |
298 | (defun gamegrid-set-display-table () | |
bf7f684c | 299 | (if (featurep 'xemacs) |
3d4ae13e RS |
300 | (add-spec-to-specifier current-display-table |
301 | gamegrid-display-table | |
302 | (current-buffer) | |
303 | nil | |
304 | 'remove-locale) | |
305 | (setq buffer-display-table gamegrid-display-table))) | |
306 | ||
3d4ae13e | 307 | (defun gamegrid-setup-default-font () |
0f7df8ac RS |
308 | (setq gamegrid-face |
309 | (copy-face 'default | |
310 | (intern (concat "gamegrid-face-" (buffer-name))))) | |
311 | (when (eq gamegrid-display-mode 'glyph) | |
312 | (let ((max-height nil)) | |
313 | (loop for c from 0 to 255 do | |
314 | (let ((glyph (aref gamegrid-display-table c))) | |
315 | (when (and (listp glyph) (eq (car glyph) 'image)) | |
316 | (let ((height (cdr (image-size glyph)))) | |
317 | (if (or (null max-height) | |
318 | (< max-height height)) | |
319 | (setq max-height height)))))) | |
320 | (when (and max-height (< max-height 1)) | |
34764b73 KH |
321 | (let ((default-font-height (face-attribute 'default :height)) |
322 | (resy (/ (display-pixel-height) (/ (display-mm-height) 25.4))) | |
323 | point-size pixel-size) | |
324 | (setq point-size (/ (* (float default-font-height) max-height) 10) | |
325 | pixel-size (floor (* resy (/ point-size 72.27))) | |
326 | point-size (* (/ pixel-size resy) 72.27)) | |
c73bd236 MB |
327 | (face-spec-set gamegrid-face |
328 | `((t :height ,(floor (* point-size 10)))))))))) | |
3d4ae13e RS |
329 | |
330 | (defun gamegrid-initialize-display () | |
331 | (setq gamegrid-display-mode (gamegrid-display-type)) | |
332 | (setq gamegrid-display-table (make-display-table)) | |
333 | (setq gamegrid-face-table (make-vector 256 nil)) | |
334 | (loop for c from 0 to 255 do | |
335 | (let* ((spec (aref gamegrid-display-options c)) | |
336 | (glyph (gamegrid-make-glyph (car spec) (caddr spec))) | |
337 | (face (gamegrid-make-face (cadr spec) (caddr spec)))) | |
338 | (aset gamegrid-face-table c face) | |
339 | (aset gamegrid-display-table c glyph))) | |
340 | (gamegrid-setup-default-font) | |
341 | (gamegrid-set-display-table) | |
0f7df8ac | 342 | (setq cursor-type nil)) |
3d4ae13e RS |
343 | |
344 | ||
345 | (defun gamegrid-set-face (c) | |
0f7df8ac RS |
346 | (if (eq gamegrid-display-mode 'glyph) |
347 | (add-text-properties (1- (point)) (point) | |
348 | (list 'display (list (aref gamegrid-display-table c)))) | |
3d4ae13e RS |
349 | (put-text-property (1- (point)) |
350 | (point) | |
351 | 'face | |
352 | (aref gamegrid-face-table c)))) | |
353 | ||
354 | (defun gamegrid-cell-offset (x y) | |
355 | (+ gamegrid-buffer-start | |
356 | (* (1+ gamegrid-buffer-width) y) | |
357 | x)) | |
358 | ||
359 | ;; ;;;;;;;;;;;;;;;; grid functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
360 | ||
361 | (defun gamegrid-get-cell (x y) | |
362 | (char-after (gamegrid-cell-offset x y))) | |
363 | ||
364 | (defun gamegrid-set-cell (x y c) | |
365 | (save-excursion | |
366 | (let ((buffer-read-only nil)) | |
367 | (goto-char (gamegrid-cell-offset x y)) | |
368 | (delete-char 1) | |
369 | (insert-char c 1) | |
370 | (gamegrid-set-face c)))) | |
371 | ||
372 | (defun gamegrid-init-buffer (width height blank) | |
373 | (setq gamegrid-buffer-width width | |
374 | gamegrid-buffer-height height) | |
375 | (let ((line (concat | |
376 | (make-string width blank) | |
377 | "\n")) | |
378 | (buffer-read-only nil)) | |
379 | (erase-buffer) | |
380 | (setq gamegrid-buffer-start (point)) | |
381 | (dotimes (i height) | |
8b78491a | 382 | (insert line)) |
0f7df8ac RS |
383 | ;; Adjust the height of the default face to the height of the |
384 | ;; images. Unlike XEmacs, Emacs doesn't allow to make the default | |
385 | ;; face buffer-local; so we do this with an overlay. | |
386 | (when (eq gamegrid-display-mode 'glyph) | |
387 | (overlay-put (make-overlay (point-min) (point-max)) | |
388 | 'face gamegrid-face)) | |
3d4ae13e RS |
389 | (goto-char (point-min)))) |
390 | ||
391 | (defun gamegrid-init (options) | |
392 | (setq buffer-read-only t | |
393 | truncate-lines t | |
82dd78a8 | 394 | line-spacing 0 |
3d4ae13e RS |
395 | gamegrid-display-options options) |
396 | (buffer-disable-undo (current-buffer)) | |
397 | (gamegrid-initialize-display)) | |
398 | ||
399 | ;; ;;;;;;;;;;;;;;;; timer functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
400 | ||
401 | (defun gamegrid-start-timer (period func) | |
402 | (setq gamegrid-timer | |
399f21c1 | 403 | (if (featurep 'xemacs) |
3d4ae13e RS |
404 | (start-itimer "Gamegrid" |
405 | func | |
406 | period | |
407 | period | |
408 | nil | |
409 | t | |
410 | (current-buffer)) | |
411 | (run-with-timer period | |
412 | period | |
413 | func | |
414 | (current-buffer))))) | |
415 | ||
416 | (defun gamegrid-set-timer (delay) | |
417 | (if gamegrid-timer | |
bf7f684c | 418 | (if (fboundp 'set-itimer-restart) |
3d4ae13e RS |
419 | (set-itimer-restart gamegrid-timer delay) |
420 | (timer-set-time gamegrid-timer | |
421 | (list (aref gamegrid-timer 1) | |
422 | (aref gamegrid-timer 2) | |
423 | (aref gamegrid-timer 3)) | |
424 | delay)))) | |
425 | ||
426 | (defun gamegrid-kill-timer () | |
427 | (if gamegrid-timer | |
399f21c1 | 428 | (if (featurep 'xemacs) |
3d4ae13e | 429 | (delete-itimer gamegrid-timer) |
d33ca4d4 | 430 | (cancel-timer gamegrid-timer))) |
3d4ae13e RS |
431 | (setq gamegrid-timer nil)) |
432 | ||
433 | ;; ;;;;;;;;;;;;;;; high score functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
434 | ||
435 | (defun gamegrid-add-score (file score) | |
f9d56d59 JB |
436 | "Add the current score to the high score file. |
437 | ||
438 | On POSIX systems there may be a shared game directory for all users in | |
439 | which the scorefiles are kept. On such systems Emacs doesn't create | |
440 | the score file FILE in this directory, if it doesn't already exist. In | |
441 | this case Emacs searches for FILE in the directory specified by | |
442 | `gamegrid-user-score-file-directory' and creates it there, if | |
443 | necessary. | |
444 | ||
445 | To add the score file for a game to the system wide shared game | |
446 | directory, create the file with the shell command \"touch\" in this | |
447 | directory and make sure that it is owned by the correct user and | |
448 | group. You probably need special user privileges to do this. | |
449 | ||
450 | On non-POSIX systems Emacs searches for FILE in the directory | |
451 | specified by the variable `temporary-file-directory'. If necessary, | |
452 | FILE is created there." | |
744ee133 CW |
453 | (case system-type |
454 | ((ms-dos windows-nt) | |
455 | (gamegrid-add-score-insecure file score)) | |
456 | (t | |
457 | (gamegrid-add-score-with-update-game-score file score)))) | |
458 | ||
f62ebc65 JB |
459 | |
460 | ;; On POSIX systems there are four cases to distinguish: | |
461 | ||
462 | ;; 1. FILE is an absolute filename. Then it should be a file in | |
463 | ;; temporary file directory. This is the way, | |
464 | ;; `gamegrid-add-score' was supposed to be used in the past and | |
465 | ;; is covered here for backward-compatibility. | |
466 | ;; | |
467 | ;; 2. The helper program "update-game-score" is setuid and the | |
468 | ;; file FILE does already exist in a system wide shared game | |
469 | ;; directory. This should be the normal case on POSIX systems, | |
470 | ;; if the game was installed system wide. Use | |
471 | ;; "update-game-score" to add the score to the file in the | |
472 | ;; shared game directory. | |
473 | ;; | |
474 | ;; 3. "update-game-score" is setuid, but the file FILE does *not* | |
475 | ;; exist in the system wide shared game directory. Use | |
476 | ;; `gamegrid-add-score-insecure' to create--if necessary--and | |
477 | ;; update FILE. This is for the case that a user has installed | |
478 | ;; a game on her own. | |
479 | ;; | |
480 | ;; 4. "update-game-score" is not setuid. Use it to create/update | |
481 | ;; FILE in the user's home directory. There is presumably no | |
482 | ;; shared game directory. | |
483 | ||
bf7f684c RS |
484 | (defvar gamegrid-shared-game-dir) |
485 | ||
744ee133 | 486 | (defun gamegrid-add-score-with-update-game-score (file score) |
f9d56d59 | 487 | (let* ((result nil) ;; What is this good for? -- os |
bf7f684c | 488 | (gamegrid-shared-game-dir |
ab8dbe67 CW |
489 | (not (zerop (logand (file-modes |
490 | (expand-file-name "update-game-score" | |
491 | exec-directory)) | |
f9d56d59 | 492 | #o4000))))) |
f62ebc65 JB |
493 | (cond ((file-name-absolute-p file) |
494 | (gamegrid-add-score-insecure file score)) | |
bf7f684c | 495 | ((and gamegrid-shared-game-dir |
f62ebc65 JB |
496 | (file-exists-p (expand-file-name file shared-game-score-directory))) |
497 | ;; Use the setuid "update-game-score" program to update a | |
498 | ;; system-wide score file. | |
bf7f684c | 499 | (gamegrid-add-score-with-update-game-score-1 file |
f62ebc65 JB |
500 | (expand-file-name file shared-game-score-directory) score)) |
501 | ;; Else: Add the score to a score file in the user's home | |
502 | ;; directory. | |
bf7f684c RS |
503 | (gamegrid-shared-game-dir |
504 | ;; If `gamegrid-shared-game-dir' is non-nil, then | |
f62ebc65 JB |
505 | ;; "update-gamescore" program is setuid, so don't use it. |
506 | (unless (file-exists-p | |
507 | (directory-file-name gamegrid-user-score-file-directory)) | |
508 | (make-directory gamegrid-user-score-file-directory t)) | |
509 | (gamegrid-add-score-insecure file score | |
510 | gamegrid-user-score-file-directory)) | |
511 | (t (let ((f (expand-file-name | |
512 | gamegrid-user-score-file-directory))) | |
513 | (when (file-writable-p f) | |
514 | (unless (eq (car-safe (file-attributes f)) | |
515 | t) | |
516 | (make-directory f)) | |
517 | (setq f (expand-file-name file f)) | |
518 | (unless (file-exists-p f) | |
519 | (write-region "" nil f nil 'silent nil 'excl))) | |
bf7f684c | 520 | (gamegrid-add-score-with-update-game-score-1 file f score)))))) |
f9d56d59 | 521 | |
bf7f684c | 522 | (defun gamegrid-add-score-with-update-game-score-1 (file target score) |
f9d56d59 | 523 | (let ((default-directory "/") |
94430ab7 EZ |
524 | (errbuf (generate-new-buffer " *update-game-score loss*")) |
525 | (marker-string (concat | |
526 | (user-full-name) | |
527 | " <" | |
528 | (cond ((fboundp 'user-mail-address) | |
529 | (user-mail-address)) | |
530 | ((boundp 'user-mail-address) | |
531 | user-mail-address) | |
532 | (t "")) | |
533 | "> " | |
534 | (current-time-string)))) | |
d33ca4d4 CY |
535 | ;; This can be called from a timer, so enable local quits. |
536 | (with-local-quit | |
537 | (apply | |
538 | 'call-process | |
539 | (append | |
540 | (list | |
541 | (expand-file-name "update-game-score" exec-directory) | |
542 | nil errbuf nil | |
543 | "-m" (int-to-string gamegrid-score-file-length) | |
544 | "-d" (if gamegrid-shared-game-dir | |
545 | (expand-file-name shared-game-score-directory) | |
546 | (file-name-directory target)) | |
547 | file | |
548 | (int-to-string score) | |
94430ab7 | 549 | marker-string)))) |
dcec69ee CW |
550 | (if (buffer-modified-p errbuf) |
551 | (progn | |
552 | (display-buffer errbuf) | |
553 | (error "Failed to update game score file")) | |
554 | (kill-buffer errbuf)) | |
d33ca4d4 | 555 | (let ((buf (find-buffer-visiting target))) |
94430ab7 EZ |
556 | (save-excursion |
557 | (if buf | |
558 | (progn | |
559 | (switch-to-buffer buf) | |
560 | (revert-buffer nil t nil) | |
561 | (display-buffer buf)) | |
562 | (find-file-read-only target)) | |
563 | (goto-char (point-min)) | |
564 | (search-forward (concat (int-to-string score) | |
565 | " " (user-login-name) " " | |
566 | marker-string)) | |
567 | (beginning-of-line))))) | |
80f60ab4 | 568 | |
f9d56d59 | 569 | (defun gamegrid-add-score-insecure (file score &optional directory) |
744ee133 | 570 | (save-excursion |
f9d56d59 JB |
571 | (setq file (expand-file-name file (or directory |
572 | temporary-file-directory))) | |
744ee133 CW |
573 | (find-file-other-window file) |
574 | (setq buffer-read-only nil) | |
575 | (goto-char (point-max)) | |
576 | (insert (format "%05d\t%s\t%s <%s>\n" | |
577 | score | |
578 | (current-time-string) | |
579 | (user-full-name) | |
580 | (cond ((fboundp 'user-mail-address) | |
581 | (user-mail-address)) | |
582 | ((boundp 'user-mail-address) | |
583 | user-mail-address) | |
584 | (t "")))) | |
cbfbd37d | 585 | (sort-fields 1 (point-min) (point-max)) |
744ee133 CW |
586 | (reverse-region (point-min) (point-max)) |
587 | (goto-line (1+ gamegrid-score-file-length)) | |
588 | (delete-region (point) (point-max)) | |
589 | (setq buffer-read-only t) | |
590 | (save-buffer))) | |
591 | ||
3d4ae13e RS |
592 | |
593 | ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
594 | ||
595 | (provide 'gamegrid) | |
6e44da43 | 596 | |
cbee283d | 597 | ;; arch-tag: a96c2ff4-1c12-427e-bd3d-faeaf174cd46 |
6e44da43 | 598 | ;;; gamegrid.el ends here |