Merge from vendor branch TNF:
[pkgsrc.git] / games / quake / patches / patch-ag
1 $NetBSD$
2
3 --- WinQuake/cd_linux.c Tue Dec 21 18:40:50 1999
4 +++ ../work/WinQuake/cd_linux.c Sat Jan 15 10:52:27 2000
5 @@ -31,7 +31,19 @@
6  #include <time.h>
7  #include <errno.h>
8  
9 +#ifdef __linux__
10  #include <linux/cdrom.h>
11 +#endif
12 +
13 +#ifdef __NetBSD__
14 +#include <sys/cdio.h>
15 +
16 +/* resume seems to have some trouble right now.  xcdplayer also can not resume
17 +   the drive correctly.  It appears to play, but no audio is heard */
18 +
19 +#define PAUSE_BY_STOPPING 1
20 +
21 +#endif
22  
23  #include "quakedef.h"
24  
25 @@ -46,165 +58,289 @@
26  static byte            playTrack;
27  static byte            maxTrack;
28  
29 +#ifdef PAUSE_BY_STOPPING
30 +static qboolean t_playLooping;
31 +static byte t_playTrack;
32 +#endif
33 +
34  static int cdfile = -1;
35 +#ifdef __linux__
36  static char cd_dev[64] = "/dev/cdrom";
37 +#endif
38 +
39 +#ifdef __NetBSD__
40 +static char cd_dev[64] = "/dev/rcd0d";
41 +#endif
42  
43  static void CDAudio_Eject(void)
44  {
45 -       if (cdfile == -1 || !enabled)
46 -               return; // no cd init'd
47 -
48 -       if ( ioctl(cdfile, CDROMEJECT) == -1 ) 
49 -               Con_DPrintf("ioctl cdromeject failed\n");
50 +#ifdef __NetBSD__
51 +  int arg=0;
52 +#endif
53 +
54 +  if (cdfile == -1 || !enabled)
55 +    return; // no cd init'd
56 +
57 +#ifdef __linux__
58 +  if ( ioctl(cdfile, CDROMEJECT) == -1 ) 
59 +    Con_DPrintf("ioctl cdromeject failed\n");
60 +#endif
61 +#ifdef __NetBSD__
62 +  if(ioctl(cdfile, DIOCLOCK, &arg) == -1)
63 +    Con_DPrintf("ioctl cdromunlock failed\n");
64 +  arg = 1;
65 +  if ( ioctl(cdfile, DIOCEJECT, &arg) == -1 ) 
66 +    Con_DPrintf("ioctl cdromeject failed\n");
67 +#endif
68  }
69  
70  
71  static void CDAudio_CloseDoor(void)
72  {
73 -       if (cdfile == -1 || !enabled)
74 -               return; // no cd init'd
75 +if (cdfile == -1 || !enabled)
76 +    return; // no cd init'd
77  
78 -       if ( ioctl(cdfile, CDROMCLOSETRAY) == -1 ) 
79 -               Con_DPrintf("ioctl cdromclosetray failed\n");
80 +#ifdef __linux__
81 +  if ( ioctl(cdfile, CDROMCLOSETRAY) == -1 ) 
82 +    Con_DPrintf("ioctl cdromclosetray failed\n");
83 +#endif
84 +#ifdef __NetBSD__
85 +  if(ioctl(cdfile, CDIOCCLOSE, NULL) == -1) {
86 +    Con_DPrintf("ioctl cdromclosetray failed\n");
87 +  }
88 +#endif
89  }
90  
91  static int CDAudio_GetAudioDiskInfo(void)
92  {
93 -       struct cdrom_tochdr tochdr;
94 +#ifdef __linux__
95 +struct cdrom_tochdr tochdr;
96 +#endif
97 +#ifdef __NetBSD__
98 +struct ioc_toc_header tochdr;
99 +#endif
100  
101 -       cdValid = false;
102 +cdValid = false;
103  
104 -       if ( ioctl(cdfile, CDROMREADTOCHDR, &tochdr) == -1 ) 
105 -    {
106 -      Con_DPrintf("ioctl cdromreadtochdr failed\n");
107 -         return -1;
108 -    }
109 +#ifdef __linux
110 +if ( ioctl(cdfile, CDROMREADTOCHDR, &tochdr) == -1 ) 
111 +{
112 +  Con_DPrintf("ioctl cdromreadtochdr failed\n");
113 +  return -1;
114 +}
115 +#endif
116  
117 -       if (tochdr.cdth_trk0 < 1)
118 -       {
119 -               Con_DPrintf("CDAudio: no music tracks\n");
120 -               return -1;
121 -       }
122 +#ifdef __NetBSD__
123 +if ( ioctl(cdfile, CDIOREADTOCHEADER, &tochdr) == -1 ) 
124 +{
125 +  Con_DPrintf("ioctl cdromreadtochdr failed\n");
126 +  return -1;
127 +}
128 +#endif
129  
130 -       cdValid = true;
131 -       maxTrack = tochdr.cdth_trk1;
132 +#ifdef __linux__
133 +if (tochdr.cdth_trk0 < 1)
134 +  {
135 +    Con_DPrintf("CDAudio: no music tracks\n");
136 +    return -1;
137 +  }
138 +#endif
139 +#ifdef __NetBSD__
140 +if (tochdr.starting_track < 1)
141 +  {
142 +    Con_DPrintf("CDAudio: no music tracks\n");
143 +    return -1;
144 +  }
145 +#endif
146 +
147 +cdValid = true;
148 +#ifdef __linux__
149 +maxTrack = tochdr.cdth_trk1;
150 +#endif
151 +#ifdef __NetBSD__
152 +maxTrack = tochdr.ending_track;
153 +#endif
154  
155 -       return 0;
156 +return 0;
157  }
158  
159  
160  void CDAudio_Play(byte track, qboolean looping)
161  {
162 -       struct cdrom_tocentry entry;
163 -       struct cdrom_ti ti;
164 +#ifdef __linux__
165 +  struct cdrom_tocentry entry;
166 +  struct cdrom_ti ti;
167 +#endif
168 +
169 +#ifdef __NetBSD__
170 +  struct ioc_read_toc_entry entry;
171 +  struct ioc_play_track ti;
172 +  int arg=1;
173 +#endif
174  
175 -       if (cdfile == -1 || !enabled)
176 -               return;
177 +  if (cdfile == -1 || !enabled)
178 +    return;
179         
180 -       if (!cdValid)
181 -       {
182 -               CDAudio_GetAudioDiskInfo();
183 -               if (!cdValid)
184 -                       return;
185 -       }
186 +  if (!cdValid)
187 +    {
188 +      CDAudio_GetAudioDiskInfo();
189 +      if (!cdValid)
190 +       return;
191 +    }
192  
193 -       track = remap[track];
194 +  track = remap[track];
195  
196 -       if (track < 1 || track > maxTrack)
197 -       {
198 -               Con_DPrintf("CDAudio: Bad track number %u.\n", track);
199 -               return;
200 -       }
201 +  if (track < 1 || track > maxTrack)
202 +    {
203 +      Con_DPrintf("CDAudio: Bad track number %u.\n", track);
204 +      return;
205 +    }
206  
207 -       // don't try to play a non-audio track
208 -       entry.cdte_track = track;
209 -       entry.cdte_format = CDROM_MSF;
210 -    if ( ioctl(cdfile, CDROMREADTOCENTRY, &entry) == -1 )
211 -       {
212 -               Con_DPrintf("ioctl cdromreadtocentry failed\n");
213 -               return;
214 -       }
215 -       if (entry.cdte_ctrl == CDROM_DATA_TRACK)
216 -       {
217 -               Con_Printf("CDAudio: track %i is not audio\n", track);
218 -               return;
219 -       }
220 +#ifdef __linux__ /* Not sure what the equiv is for NetBSD */
221 +  /* don't try to play a non-audio track */
222 +  entry.cdte_track = track;
223 +  entry.cdte_format = CDROM_MSF;
224 +  if ( ioctl(cdfile, CDROMREADTOCENTRY, &entry) == -1 )
225 +    {
226 +      Con_DPrintf("ioctl cdromreadtocentry failed\n");
227 +      return;
228 +    }
229 +  if (entry.cdte_ctrl == CDROM_DATA_TRACK)
230 +    {
231 +      Con_Printf("CDAudio: track %i is not audio\n", track);
232 +      return;
233 +    }
234  
235 -       if (playing)
236 -       {
237 -               if (playTrack == track)
238 -                       return;
239 -               CDAudio_Stop();
240 -       }
241 +#endif
242  
243 -       ti.cdti_trk0 = track;
244 -       ti.cdti_trk1 = track;
245 -       ti.cdti_ind0 = 1;
246 -       ti.cdti_ind1 = 99;
247 +  if (playing)
248 +    {
249 +      if (playTrack == track)
250 +       return;
251 +      CDAudio_Stop();
252 +    }
253  
254 -       if ( ioctl(cdfile, CDROMPLAYTRKIND, &ti) == -1 ) 
255 +#ifdef __linux__
256 +  ti.cdti_trk0 = track;
257 +  ti.cdti_trk1 = track;
258 +  ti.cdti_ind0 = 1;
259 +  ti.cdti_ind1 = 99;
260 +  if ( ioctl(cdfile, CDROMPLAYTRKIND, &ti) == -1 ) 
261      {
262 -               Con_DPrintf("ioctl cdromplaytrkind failed\n");
263 -               return;
264 +      Con_DPrintf("ioctl cdromplaytrkind failed\n");
265 +      return;
266      }
267  
268 -       if ( ioctl(cdfile, CDROMRESUME) == -1 ) 
269 -               Con_DPrintf("ioctl cdromresume failed\n");
270 +  if ( ioctl(cdfile, CDROMRESUME) == -1 ) 
271 +    Con_DPrintf("ioctl cdromresume failed\n");
272 +#endif
273 +
274 +#ifdef __NetBSD__
275 +  ti.start_track = track;
276 +  ti.end_track = track;
277 +  ti.start_index = 1;
278 +  ti.end_index = 99;
279 +  if ( ioctl(cdfile, CDIOCPLAYTRACKS, &ti) == -1 ) 
280 +    {
281 +      Con_DPrintf("ioctl cdromplaytrkind failed\n");
282 +      return;
283 +    }
284  
285 -       playLooping = looping;
286 -       playTrack = track;
287 -       playing = true;
288 +  if ( ioctl(cdfile, CDIOCRESUME, &arg) == -1 ) 
289 +    Con_DPrintf("ioctl cdromresume failed\n");
290 +#endif
291 +
292 +  playLooping = looping;
293 +  playTrack = track;
294 +  playing = true;
295  
296 -       if (cdvolume == 0.0)
297 -               CDAudio_Pause ();
298 +  if (cdvolume == 0.0)
299 +    CDAudio_Pause ();
300  }
301  
302  
303  void CDAudio_Stop(void)
304  {
305 -       if (cdfile == -1 || !enabled)
306 -               return;
307 +  if (cdfile == -1 || !enabled)
308 +    return;
309         
310 -       if (!playing)
311 -               return;
312 +  if (!playing)
313 +    return;
314  
315 -       if ( ioctl(cdfile, CDROMSTOP) == -1 )
316 -               Con_DPrintf("ioctl cdromstop failed (%d)\n", errno);
317 +#ifdef __linux__
318 +  if ( ioctl(cdfile, CDROMSTOP) == -1 )
319 +    Con_DPrintf("ioctl cdromstop failed (%d)\n", errno);
320 +#endif
321 +#ifdef __NetBSD__
322 +  if ( ioctl(cdfile, CDIOCSTOP) == -1 )
323 +    Con_DPrintf("ioctl cdromstop failed (%d)\n", errno);
324 +#endif
325  
326 -       wasPlaying = false;
327 -       playing = false;
328 +  wasPlaying = false;
329 +  playing = false;
330  }
331  
332  void CDAudio_Pause(void)
333  {
334 -       if (cdfile == -1 || !enabled)
335 -               return;
336 -
337 -       if (!playing)
338 -               return;
339 +#ifdef PAUSE_BY_STOPPING
340 +  qboolean t;
341 +#endif
342 +
343 +  if (cdfile == -1 || !enabled)
344 +    return;
345 +
346 +  if (!playing)
347 +    return;
348 +
349 +#ifndef PAUSE_BY_STOPPING
350 +#ifdef __linux__
351 +  if ( ioctl(cdfile, CDROMPAUSE) == -1 ) 
352 +    Con_DPrintf("ioctl cdrompause failed\n");
353 +#endif
354 +
355 +#ifdef __NetBSD__
356 +  if ( ioctl(cdfile, CDIOCPAUSE) == -1 ) 
357 +    Con_DPrintf("ioctl cdrompause failed\n");
358 +#endif
359 +#else
360 +  t_playLooping = playLooping;
361 +  t_playTrack=playTrack;
362 +  t=playing;
363 +  CDAudio_Stop();
364 +  playing=t;
365 +#endif
366  
367 -       if ( ioctl(cdfile, CDROMPAUSE) == -1 ) 
368 -               Con_DPrintf("ioctl cdrompause failed\n");
369 -
370 -       wasPlaying = playing;
371 -       playing = false;
372 +  wasPlaying = playing;
373 +  playing = false;
374  }
375  
376  
377  void CDAudio_Resume(void)
378  {
379 -       if (cdfile == -1 || !enabled)
380 -               return;
381 +  if (cdfile == -1 || !enabled)
382 +    return;
383         
384 -       if (!cdValid)
385 -               return;
386 +  if (!cdValid)
387 +    return;
388  
389 -       if (!wasPlaying)
390 -               return;
391 +  if (!wasPlaying)
392 +    return;
393         
394 -       if ( ioctl(cdfile, CDROMRESUME) == -1 ) 
395 -               Con_DPrintf("ioctl cdromresume failed\n");
396 -       playing = true;
397 +#ifndef PAUSE_BY_STOPPING
398 +#ifdef __linux__
399 +  if ( ioctl(cdfile, CDROMRESUME) == -1 ) 
400 +    Con_DPrintf("ioctl cdromresume failed\n");
401 +#endif
402 +
403 +#ifdef __NetBSD__
404 +  if ( ioctl(cdfile, CDIOCRESUME) == -1 ) 
405 +    Con_DPrintf("ioctl cdromresume failed\n");
406 +#endif
407 +#else
408 +  CDAudio_Play(t_playTrack, t_playLooping);
409 +#endif
410 +
411 +  playing = true;
412  }
413  
414  static void CD_f (void)
415 @@ -327,43 +463,72 @@
416  
417  void CDAudio_Update(void)
418  {
419 -       struct cdrom_subchnl subchnl;
420 -       static time_t lastchk;
421 +#ifdef __linux__
422 +  struct cdrom_subchnl subchnl;
423 +#endif
424 +#ifdef __NetBSD__
425 +  struct ioc_read_subchannel subchnl;
426 +  struct cd_sub_channel_info data;
427 +#endif
428 +  static time_t lastchk;
429  
430 -       if (!enabled)
431 -               return;
432 +  if (!enabled)
433 +    return;
434  
435 -       if (bgmvolume.value != cdvolume)
436 +  if (bgmvolume.value != cdvolume)
437 +    {
438 +      if (cdvolume)
439         {
440 -               if (cdvolume)
441 -               {
442 -                       Cvar_SetValue ("bgmvolume", 0.0);
443 -                       cdvolume = bgmvolume.value;
444 -                       CDAudio_Pause ();
445 -               }
446 -               else
447 -               {
448 -                       Cvar_SetValue ("bgmvolume", 1.0);
449 -                       cdvolume = bgmvolume.value;
450 -                       CDAudio_Resume ();
451 -               }
452 +         Cvar_SetValue ("bgmvolume", 0.0);
453 +         cdvolume = bgmvolume.value;
454 +         CDAudio_Pause ();
455         }
456 -
457 -       if (playing && lastchk < time(NULL)) {
458 -               lastchk = time(NULL) + 2; //two seconds between chks
459 -               subchnl.cdsc_format = CDROM_MSF;
460 -               if (ioctl(cdfile, CDROMSUBCHNL, &subchnl) == -1 ) {
461 -                       Con_DPrintf("ioctl cdromsubchnl failed\n");
462 -                       playing = false;
463 -                       return;
464 -               }
465 -               if (subchnl.cdsc_audiostatus != CDROM_AUDIO_PLAY &&
466 -                       subchnl.cdsc_audiostatus != CDROM_AUDIO_PAUSED) {
467 -                       playing = false;
468 -                       if (playLooping)
469 -                               CDAudio_Play(playTrack, true);
470 -               }
471 +      else
472 +       {
473 +         Cvar_SetValue ("bgmvolume", 1.0);
474 +         cdvolume = bgmvolume.value;
475 +         CDAudio_Resume ();
476         }
477 +    }
478 +
479 +
480 +  if (playing && lastchk < time(NULL)) {
481 +    lastchk = time(NULL) + 2;    /* two seconds between chks */
482 +#ifdef __linux
483 +    subchnl.cdsc_format = CDROM_MSF;
484 +    if (ioctl(cdfile, CDROMSUBCHNL, &subchnl) == -1 ) {
485 +      Con_DPrintf("ioctl cdromsubchnl failed\n");
486 +      playing = false;
487 +      return;
488 +    }
489 +    if (subchnl.cdsc_audiostatus != CDROM_AUDIO_PLAY &&
490 +       subchnl.cdsc_audiostatus != CDROM_AUDIO_PAUSED) {
491 +      playing = false;
492 +      if (playLooping)
493 +       CDAudio_Play(playTrack, true);
494 +    }
495 +#endif
496 +#ifdef __NetBSD__
497 +    subchnl.address_format = CD_MSF_FORMAT;
498 +    subchnl.data_format = CD_CURRENT_POSITION;
499 +    subchnl.data_len = sizeof(data);
500 +    subchnl.track = playTrack;
501 +    subchnl.data = &data;
502 +
503 +    if (ioctl(cdfile, CDIOCREADSUBCHANNEL, &subchnl) == -1 ) {
504 +      Con_DPrintf("ioctl cdromsubchnl failed\n");
505 +      playing = false;
506 +      return;
507 +    }
508 +    if (subchnl.data->header.audio_status != CD_AS_PLAY_IN_PROGRESS &&
509 +       subchnl.data->header.audio_status != CD_AS_PLAY_PAUSED) {
510 +      playing = false;
511 +      if (playLooping)
512 +       CDAudio_Play(playTrack, true);
513 +    }
514 +#endif
515 +  }
516 +
517  }
518  
519  int CDAudio_Init(void)
520 @@ -382,9 +547,9 @@
521         }
522  
523         if ((cdfile = open(cd_dev, O_RDONLY)) == -1) {
524 -               Con_Printf("CDAudio_Init: open of \"%s\" failed (%i)\n", cd_dev, errno);
525 -               cdfile = -1;
526 -               return -1;
527 +         Con_Printf("CDAudio_Init: open of \"%s\" failed (%i)\n", cd_dev, errno);
528 +         cdfile = -1;
529 +         return -1;
530         }
531  
532         for (i = 0; i < 100; i++)