File: workbone.c

package info (click to toggle)
workman 1.3b-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,172 kB
  • ctags: 896
  • sloc: ansic: 11,084; makefile: 123; sh: 28
file content (531 lines) | stat: -rw-r--r-- 11,873 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/* WorkBone CD Rom Player interface for WorkMan

   Copyright (c) 1996,1997 Dirk Foersterling
   WorkBone: Copyright (c) 1994  Thomas McWilliams

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#if !defined( linux )
#error ----------------------------------------
#error WorkBone is designed for Linux.
#error If you can program your platform, please
#error take a look at the source and try to get
#error WorkBone running. Thanks.
#error (report your results to
#error milliByte@DeathsDoor.com please)
#error ----------------------------------------
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <signal.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <termios.h>
#include <mntent.h>
#include <getopt.h>
#include "workbone.h"
#include "config.h"
#include "struct.h"

#include "cdrom.h"
#include "plat_.h"
#include "database.h"
#include "cdinfo.h"


static char *workbone_id="$Id: workbone.c,v 1.2 1997/12/20 10:15:40 dirk Exp $";

void control_panel (void);
void rl_ttyset (int Reset);
void help (void);
static inline void playtime (void);
static inline int kvers (void);
static inline void setgraf (void);
static inline void cls (void);

int cur_balance = 10, info_modified;

char tmptime[100];
char *tottime;
static int kern = 0;
static int usegraf = FALSE;
static char gon[100];
static char gof[100];

const char *wb_hdr[] =
{
  "", WORKMAN_VERSION_STRING,
  "Copyright 1997 (c) Dirk F�rsterling",
  "WorkBone interface Copyright 1994 (c) Thomas McWilliams",
  "Free Software under GNU General Public License. NO WARRANTY.\n",
  NULL};

int
main (int argc, char *argv[])
{

  int sss, sel_stat, dly;
  int fastin = FALSE;
  int scmd = 0, tmppos = 0;
  int save_track = 1;
  fd_set rset;
  struct timeval mydelay;
  struct mntent *mnt;
  char copt;
  FILE *fp;
  thiscd.trk = NULL;
  thiscd.lists = NULL;
  mydelay.tv_sec = 0;
  mydelay.tv_usec = 500000;	/* initial delay 1/2 sec */
  tottime = tmptime;
  kern = kvers ();


  /* get options */
  for (;;)
    {
      if ((copt = getopt (argc, argv, "d:ghqtvV")) == EOF)
	break;
      switch (copt)
	{
        case 'd':
          cd_device = optarg;
          break;
	case 'g':
	  usegraf = TRUE;
	  break;
	case 'q':
	  fastin = TRUE;
	  break;
	case 't':
	  show_terms (wb_hdr);
	  exit (0);
	default:
	  show_terms (wb_hdr);
	  printf ("Usage:  workbone [ -v | -h | -q | -a | -d<device> ]\n");
	  printf ("           -v : version\n");
	  printf ("           -h : help\n");
          printf ("   -d<device> : Use <device> as cd device (default: %s)\n",cd_device);
	  printf ("           -q : fast start (no wait for init)\n");
	  printf ("           -g : use 8-bit ascii graphics for display\n");
	  printf ("           -t : show license and no warranty\n\n");
	  printf ("Type 'workbone' to start program.\n\n");
	  printf ("Engage the NUM LOCK on your keypad. From inside WorkBone\n");
	  printf ("pressing DEL on numeric keypad will display a help menu.\n\n");
	  exit (0);
	}
    }
  /* set graphics */
  setgraf ();
  /* check if drive is mounted (from Mark Buckaway's cdplayer code) */
  /* Changed it again (look at XPLAYCD from ????                    */
  /* It's better to check the device name rather than one device is */
  /* mounted as iso9660. That prevents "no playing" if you have more*/
  /* than one CD-ROM, and one of them is mounted, but it's not the  */
  /* audio CD                                              -dirk    */
  if ((fp = setmntent (MOUNTED, "r")) == NULL)
    {
      fprintf (stderr, "Couldn't open %s: %s\n", MOUNTED, strerror (errno));
      exit (1);
    }
  while ((mnt = getmntent (fp)) != NULL)
    {
      if (strcmp (mnt->mnt_fsname, cd_device) == 0)
	{
	  fputs ("CDROM already mounted. Operation aborted.\n", stderr);
	  endmntent (fp);
	  exit (1);
	}
    }
  endmntent (fp);

  /*  display control panel template */
  control_panel ();

  /* delay while CD drive initializes itself */
  if (!fastin)
    for (dly = 6; dly > -1; dly--)
      {
	printf (MTAB3 "wait ... initializing %d\r", dly);
	fflush (stdout);
	sleep (1);
      }

  printf (MTAB3 "                         \r");
  fflush (stdout);
  sss = cd_status ();
  signal (SIGINT, SIG_IGN);
  rl_ttyset (0);
  if (sss == 0 || sss == 4)
    goto done;
  cur_track = 1;

  do
    {
/*
   use select() to update status 
 */

      FD_ZERO (&rset);
      FD_SET (STDIN_FILENO, &rset);
      sel_stat = select (4, &rset, NULL, NULL, &mydelay);
      if (sel_stat == 0)
	{
	  mydelay.tv_sec = WBS_DELAY;
	  mydelay.tv_usec = WBU_DELAY;
	}
      sss = cd_status ();
      if (sss == 0 || sss == 4 || cur_cdmode == EJECTED)
	{
	  scmd = '0';
	  goto done;
	}
      if (cur_cdmode < 1)
	save_track = 1;
      /* if key was pressed, parse it and do function */
      
      if (FD_ISSET (STDIN_FILENO, &rset))
	{
	  read (0, &scmd, 1);

	  switch (scmd & 255)
	    {
	    case '.':
	    case '?':
	      help ();
	      break;
	    case '1':
	      if (cur_cdmode == PLAYING)
		{
		  tmppos = cur_pos_rel - 15;
		  play_cd (cur_track, tmppos > 0 ? tmppos : 0, cur_ntracks + 1);
		  mydelay.tv_sec = 0;
		}
	      break;
	    case '3':
	      if (cur_cdmode == PLAYING)
		{
		  tmppos = cur_pos_rel + 15;
		  if (tmppos < thiscd.trk[cur_track - 1].length)
		    {
		      play_cd (cur_track, tmppos, cur_ntracks + 1);
		      mydelay.tv_sec = 0;
		    }
		}
	      break;
	    case '2':
	      stop_cd ();
	      eject_cd ();
	      break;
	    case '4':
	      cur_track--;
	      if (cur_track < 1)
		cur_track = cur_ntracks;
	      play_cd (cur_track, 0, cur_ntracks + 1);
	      mydelay.tv_sec = 0;
	      break;
	    case '5':
	      if (cur_cdmode == PLAYING)
		play_cd (cur_track, 0, cur_ntracks + 1);
	      mydelay.tv_sec = 0;
	      break;
	    case '6':
	      if (cur_track == cur_ntracks)
		cur_track = 0;
	      play_cd (cur_track + 1, 0, cur_ntracks + 1);
	      mydelay.tv_sec = 0;
	      break;
	    case '7':
	      printf (MTAB3 "stop                         \r");
	      save_track = cur_track;
	      stop_cd ();
	      break;
	    case '8':
	      if (cur_cdmode == PLAYING || cur_cdmode == PAUSED)
		{
		  pause_cd ();
		}
	      break;
	    case '9':
	      if (cur_cdmode == STOPPED || cur_cdmode == TRACK_DONE)
		{
		  play_cd (save_track, 0, cur_ntracks + 1);
		  mydelay.tv_sec = 0;
		}
	      break;
	    case 0x1b:
	      read (0, &scmd, 1);
	      read (0, &scmd, 1);
	      printf (MTAB3 "Turn ON Num Lock!                    %c\r", 7);
	      fflush (stdout);
	      sleep (1);
	      printf (MTAB3 "                        \r");
	      fflush (stdout);
	      break;
	    default:
	      break;
	    }
	}
      /* update display of which track is playing */

      switch (cur_cdmode)
	{
	case TRACK_DONE:
	  cur_track = save_track = 1;
	  printf (MTAB3 "stopped                    \r");
	  break;
	case PLAYING:
	  playtime ();
	  printf (MTAB3 "playing #%d%s       \r", cur_track, tottime);
	  break;
	case PAUSED:
	  printf (MTAB3 "pause   #%d  \r", cur_track);
	  break;
	case STOPPED:
	  printf (MTAB3 "stopped #%d \r", save_track);
	  break;
	case EJECTED:
	  goto done;
	default:
	  printf (MTAB3 "cur_cdmode %d       \r", cur_cdmode);
	}
      fflush (stdout);

    }
  while (scmd != '0');
done:
  if (thiscd.trk != NULL)
    free (thiscd.trk);
  printf ("\n");
  rl_ttyset (1);
  signal (SIGINT, SIG_DFL);
  return (0);
}

/* takes terminal in and out of raw mode */
void
rl_ttyset (int Reset)
{
  static struct termios old;
  struct termios new;

  if (Reset == 0)
    {
      (void) tcgetattr (0, &old);
      new = old;
      new.c_lflag &= ~(ECHO | ICANON);
      new.c_iflag &= ~(ISTRIP | INPCK);
      (void) tcsetattr (0, TCSANOW, &new);
    }
  else
    (void) tcsetattr (0, TCSANOW, &old);
}


/* Copy into a malloced string. */
void
strmcpy (char **t, char *s)
{
  if (*t != NULL)
    free (*t);

  *t = malloc (strlen (s) + 1);
  if (*t == NULL)
    {
      perror ("strmcpy");
      exit (1);
    }

  (void) strcpy (*t, s);
}

/* display keypad template on screen */
void
control_panel ()
{
  if (usegraf)
    {
      printf ("%s\n", gon);
      printf (ROWT);
      printf (ROW0);
      printf (ROW1);
      printf (ROW0);
      printf (ROW2);
      printf (ROW0);
      printf (ROW3);
      printf (ROW0);
      printf (ROW4);
      printf (ROW0);
      printf (ROWB, gof);
    }
  else
    {
      printf ("\n");
      printf (AROWT);
      printf (AROW0);
      printf (AROW1);
      printf (AROW0);
      printf (AROW2);
      printf (AROW0);
      printf (AROW3);
      printf (AROW0);
      printf (AROW4);
      printf (AROW0);
      printf (AROWB);
    }
}

/* ansi codes to clear screen */
static void
cls (void)
{
  if (usegraf)
    {
      printf ("\033[2J\033[H");
    }
  else
    {
      int count = 60;
      while (count--)
	printf ("\n");
    }
}

/* print help screen */
void
help ()
{
  const char *glst[] =
  {
    "\376\376", "\tstop                       ",
    "||", "\tpause/resume",
    "|\020", "\tplay",
    "\036\036", "\tre-start current selection",
    "|\021", "\tprevious selection",
    "\020|", "\tnext selection",
    "\021\021", "\tgo backward 15 seconds",
    "\020\020", "\tgo foreward 15 seconds",
    "..", "\tabort workbone",
    "quit", "\texit workbone (music continues)",
    "?", "\tdisplay help screen", NULL};

  const char *alst[] =
  {
    "[]", "\tstop                       ",
    "||", "\tpause/resume",
    "=>", "\tplay",
    "^^", "\tre-start current selection",
    "< ", "\tprevious selection",
    "> ", "\tnext selection",
    "<<", "\tgo backward 15 seconds",
    ">>", "\tgo foreward 15 seconds",
    "..", "\tabort workbone",
    "quit", "\texit workbone (music continues)",
    "?", "\tdisplay help screen", NULL};

  int i = 0;
  cls ();
  while (glst[i] != NULL)
    {
      printf ("\t\t\t%s%s%s%s\n", gon, (usegraf ? glst[i] : alst[i]),
	      gof, (usegraf ? glst[i + 1] : alst[i + 1]));
      i += 2;
    }
  control_panel ();
}

inline void
playtime (void)
{
  static int mymin, emin;
  static int mysec, esec;
  int tmp = 0;

  if (cur_pos_rel > 0 && (tmp = cur_pos_rel % 60) == mysec)
    return;
  mysec = tmp;
  mymin = cur_pos_rel / 60;
  esec = cur_pos_abs % 60;
  emin = cur_pos_abs / 60;
  sprintf (tmptime, "%s %02d:%02d  %02d:%02d", cur_track > 9 ? " " : "  ",
	   mymin, mysec, emin, esec);
  return;
}

inline int
kvers (void)
{
  struct utsname ubf;
  uname (&ubf);
  if (ubf.release[0] == '1' && ubf.release[2] == '0')
    return 0;
  else
    return 1;
}

static inline void
setgraf (void)
{
  if (usegraf)
    {
      strcpy (gon, (kern == 0 ? OGON : GON));
      strcpy (gof, (kern == 0 ? OGOF : GOF));
    }
  else
    {
      gon[0] = 0;
      gof[0] = 0;
    }
}

void
show_terms (const char **p)
{
  int i = 0;

  while (p[i] != NULL)
    {
      puts (p[i]);
      i++;
    }
}


void about_set_drivetype(unsigned char *vendor, unsigned char *model,
                         unsigned char *rev )
{
	return;
}                         

/*
 * We don't use the database. So load() has to be defined to satisfy
 * cdrom.c's call to load()
 */
void load( void )
{
   return;
}
  
/*
 * The following function to satisfy cc and ld
 */

int big_spaces = 0;