wmshutdown: Add icon for freedesktop.org icon themes.
[dockapps.git] / ascd / libworkman / plat_news.c
blobcad7bb98e8b014d9d1801a18ccea80473f30ebd0
1 /*
2 * $Id: plat_news.c,v 1.7 1999/03/07 08:36:40 dirk Exp $
4 * This file is part of WorkMan, the civilized CD player library
5 * (c) 1991-1997 by Steven Grimm (original author)
6 * (c) by Dirk Försterling (current 'author' = maintainer)
7 * The maintainer can be contacted by his e-mail address:
8 * milliByte@DeathsDoor.com
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the Free
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Sony NEWS-specific drive control routines.
28 #if defined( __sony_news) || defined(sony_news)
30 static char plat_news_id[] = "$Id: plat_news.c,v 1.7 1999/03/07 08:36:40 dirk Exp $";
32 #include <errno.h>
33 #include <stdio.h>
34 #include <fcntl.h>
35 #include <ustat.h>
36 #include <CD.h>
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include <sys/time.h>
42 #include "include/wm_config.h"
43 #include "include/wm_struct.h"
45 #define WM_MSG_CLASS WM_MSG_CLASS_PLATFORM
47 void *malloc();
48 char *strchr();
50 extern char *cd_device;
51 extern int intermittent_dev;
53 int min_volume = 128;
54 int max_volume = 255;
57 * Initialize the drive. A no-op for the generic driver.
59 int
60 gen_init(d)
61 struct wm_drive *d;
63 return (0);
67 * Get the number of tracks on the CD.
69 int
70 gen_get_trackcount(d, tracks)
71 struct wm_drive *d;
72 int *tracks;
74 struct CD_Capacity cc;
76 if (CD_GetCapacity(d->fd, &cc))
77 return (-1);
79 *tracks = cc.etrack - 1;
80 return (0);
84 * Get the start time and mode (data or audio) of a track.
86 int
87 gen_get_trackinfo(d, track, data, startframe)
88 struct wm_drive *d;
89 int track, *data, *startframe;
91 struct CD_TOCinfo hdr;
92 struct CD_TOCdata ent;
94 hdr.strack = track;
95 hdr.ntrack = 1;
96 hdr.data = &ent;
97 if (CD_ReadTOC(d->fd, &hdr))
98 return (-1);
100 *data = (ent.control & 4) ? 1 : 0;
101 *startframe = ent.baddr;
103 return (0);
107 * Get the number of frames on the CD.
110 gen_get_cdlen(d, frames)
111 struct wm_drive *d;
112 int *frames;
114 int tmp;
116 if ((d->get_trackcount)(d, &tmp))
117 return (-1);
119 return (gen_get_trackinfo(d, tmp + 1, &tmp, frames));
123 * Get the current status of the drive: the current play mode, the absolute
124 * position from start of disc (in frames), and the current track and index
125 * numbers if the CD is playing or paused.
128 gen_get_drive_status(d, oldmode, mode, pos, track, index)
129 struct wm_drive *d;
130 enum wm_cd_modes oldmode, *mode;
131 int *pos, *track, *index;
133 struct CD_Status sc;
135 /* If we can't get status, the CD is ejected, so default to that. */
136 *mode = WM_CDM_EJECTED;
138 /* Is the device open? */
139 if (d->fd < 0)
141 switch (wmcd_open(d)) {
142 case -1: /* error */
143 return (-1);
145 case 1: /* retry */
146 return (0);
150 /* Disc is ejected. Close the device. */
151 if (CD_GetStatus(d->fd, &sc))
153 WMclose(d->fd);
154 d->fd = -1;
155 return (0);
158 switch (sc.status) {
159 case CDSTAT_PLAY:
160 *mode = PLAYING;
161 *track = sc.tno;
162 *index = sc.index;
163 *pos = sc.baddr;
164 break;
166 case CDSTAT_PAUSE:
167 if (oldmode == WM_CDM_PLAYING || oldmode == WM_CDM_PAUSED)
169 *mode = WM_CDM_PAUSED;
170 *track = sc.tno;
171 *index = sc.index;
172 *pos = sc.baddr;
174 else
175 *mode = WM_CDM_STOPPED;
176 break;
178 case CDSTAT_STOP:
179 if (oldmode == WM_CDM_PLAYING)
181 *mode = WM_CDM_TRACK_DONE; /* waiting for next track. */
182 break;
184 /* fall through */
186 default:
187 *mode = WM_CDM_STOPPED;
188 break;
191 return (0);
195 * Set the volume level for the left and right channels. Their values
196 * range from 0 to 100.
199 gen_set_volume(d, left, right)
200 struct wm_drive *d;
201 int left, right;
203 /* NEWS can't adjust volume! */
204 return (0);
208 * Pause the CD.
211 gen_pause(d)
212 struct wm_drive *d;
214 CD_Pause(d->fd);
216 return (0);
220 * Resume playing the CD (assuming it was paused.)
223 gen_resume(d)
224 struct wm_drive *d;
226 CD_Restart(d->fd);
228 return (0);
232 * Stop the CD.
235 gen_stop(d)
236 struct wm_drive *d;
238 CD_Stop(d->fd);
240 return (0);
244 * Play the CD from one position to another (both in frames.)
247 gen_play(d, start, end)
248 struct wm_drive *d;
249 int start, end;
251 struct CD_PlayAddr msf;
253 msf.addrmode = CD_MSF;
254 msf.addr.msf.startmsf.min = start / (60*75);
255 msf.addr.msf.startmsf.sec = (start % (60*75)) / 75;
256 msf.addr.msf.startmsf.frame = start % 75;
257 msf.addr.msf.endmsf.min = end / (60*75);
258 msf.addr.msf.endmsf.sec = (end % (60*75)) / 75;
259 msf.addr.msf.endmsf.frame = end % 75;
261 if (CD_Play(d->fd, &msf))
263 printf("wm_cd_play_chunk(%d,%d)\n",start,end);
264 printf("msf = %d:%d:%d %d:%d:%d\n",
265 msf.addr.msf.startmsf.min,
266 msf.addr.msf.startmsf.sec,
267 msf.addr.msf.startmsf.frame,
268 msf.addr.msf.endmsf.min,
269 msf.addr.msf.endmsf.sec,
270 msf.addr.msf.endmsf.frame);
271 perror("CD_Play");
272 return (-1);
275 return (0);
279 * Eject the current CD, if there is one.
282 gen_eject(d)
283 struct wm_drive *d;
285 struct stat stbuf;
286 struct ustat ust;
288 if (fstat(d->fd, &stbuf) != 0)
289 return (-2);
291 /* Is this a mounted filesystem? */
292 if (ustat(stbuf.st_rdev, &ust) == 0)
293 return (-3);
295 if (CD_AutoEject(d->fd))
296 return (-1);
298 /* Close the device if it needs to vanish. */
299 if (intermittent_dev)
301 WMclose(d->fd);
302 d->fd = -1;
305 return (0);
308 /*----------------------------------------*
309 * Close the CD tray
311 * Please edit and send changes to
312 * milliByte@DeathsDoor.com
313 *----------------------------------------*/
315 int gen_closetray(struct wm_drive *d)
317 #ifdef CAN_CLOSE
318 if(!close(d->fd))
320 d->fd=-1;
321 return(wmcd_reopen(d));
322 } else {
323 return(-1);
325 #else
326 /* Always succeed if the drive can't close */
327 return(0);
328 #endif /* CAN_CLOSE */
329 } /* gen_closetray() */
333 * Close the CD device.
336 WMclose(fd)
337 int fd;
339 int ret;
341 ret = CD_Close(fd);
342 wm_susleep(3000000);
343 return (ret);
347 * Read the initial volume from the drive, if available. Each channel
348 * ranges from 0 to 100, with -1 indicating data not available.
351 gen_get_volume(d, left, right)
352 struct wm_drive *d;
353 int *left, *right;
355 /* Suns, HPs, Linux, NEWS can't read the volume; oh well */
356 *left = *right = -1;
358 return (0);
362 * Pass SCSI commands to the device.
365 wm_scsi(d, cdb, cdblen, buf, buflen, getreply)
366 struct wm_drive *d;
367 unsigned char *cdb, *buf;
368 int cdblen, buflen, getreply;
370 /* NEWS can't do SCSI passthrough... or can it? */
371 return (-1);
375 * Open the CD device and figure out what kind of drive is attached.
378 wmcd_open(d)
379 struct wm_drive *d;
381 int fd;
382 static int warned = 0;
383 char vendor[32] = WM_STR_GENVENDOR;
384 char model[32] = WM_STR_GENMODEL;
385 char rev[32] = WM_STR_GENREV;
387 if (d->fd >= 0) /* Device already open? */
388 return (0);
390 intermittent_dev = 1;
391 if (cd_device == NULL)
392 cd_device = DEFAULT_CD_DEVICE;
394 if ((d->fd = CD_Open(cd_device, 0)) < 0)
396 /* Solaris 2.2 volume manager moves links around */
397 if (errno == ENOENT && intermittent_dev)
398 return (0);
400 if (errno == EACCES)
402 if (!warned)
404 char realname[MAXPATHLEN];
406 if (realpath(cd_device, realname) == NULL)
408 perror("realpath");
409 return (0);
412 fprintf(stderr,
413 "As root, please run\n\nchmod 666 %s\n\n%s\n", realname,
414 "to give yourself permission to access the CD-ROM device.");
415 warned++;
418 else if (errno != EIO) /* defined at top */
420 perror(cd_device);
421 exit(1);
424 /* No CD in drive. */
425 return (1);
428 if (warned)
430 warned = 0;
431 fprintf(stderr, "Thank you.\n");
434 /* Now fill in the relevant parts of the wm_drive structure. */
435 fd = d->fd;
437 /* Figure out the drive type, if possible */
438 wm_scsi_get_drive_type(d, vendor, model, rev);
439 *d = *(find_drive_struct(vendor, model, rev));
440 wm_drive_settype(vendor, model, rev);
442 d->fd = fd;
444 (d->init)(d);
446 return (0);
447 } /* wmcd_open() */
450 * Re-Open the device if it is open.
453 wmcd_reopen( struct wm_drive *d )
455 int status;
457 do {
458 wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "wmcd_reopen ");
459 if (d->fd >= 0) /* Device really open? */
461 wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "closes the device and ");
462 status = close( d->fd ); /* close it! */
463 /* we know, that the file is closed, do we? */
464 d->fd = -1;
466 wm_susleep( 1000 );
467 wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "calls wmcd_open()\n");
468 status = wmcd_open( d ); /* open it as usual */
469 wm_susleep( 1000 );
470 } while ( status != 0 );
471 return status;
472 } /* wmcd_reopen() */
474 #endif