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