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