/*
 * Copyright 1988, 1989, 1990, 1991 Massachusetts Institute of Technology
 */
#include"xpointer.h"
#include"../../Glib/Glib.h"

do_shuttle(event)
     XEvent event;
{
  extern int cur_volume, cur_speed;
  extern short DEBUG;
  extern Display *dpy;
  extern Server *serv;
  extern GC black_gc, normtext, brighttext;
  extern char cur_speed_string[];
  extern Window speed_win;

  Window root_return, child_return, active_win;
  int root_x_return, root_y_return, win_x_return, win_y_return;
  unsigned int mask_return;

  active_win = event.xany.window;
  GSetState((int)DEBUG,0,ASYNC_MODE);
  cur_speed = atoi(cur_speed_string);
  XFillRectangle( dpy, active_win, black_gc,
		 cur_speed+200, 0, 5, 40);
  while (event.type != ButtonRelease) {
    XQueryPointer( dpy, active_win, &root_return, &child_return,
		  &root_x_return, &root_y_return, &win_x_return, &win_y_return,
		  &mask_return);
    if (cur_speed != ((win_x_return+5)/10)*10-200) {
      XFillRectangle( dpy, active_win, black_gc,
		     cur_speed+200, 0, 5, 40);
      cur_speed = ((win_x_return+5)/10)*10-200;    
      GRun(serv, GVolumeIndex(serv,cur_volume), cur_speed, 0);
      sprintf(cur_speed_string,"%d",cur_speed);
      show_direct( speed_win);
      if (DEBUG) printf("Speed is %d\n",cur_speed);
      XFillRectangle( dpy, active_win, brighttext,
		     cur_speed+200, 0, 5, 40);
    }
    XMaskEvent( dpy, ButtonReleaseMask | PointerMotionMask, &event);
  }
  XFillRectangle( dpy, active_win, black_gc,
		 cur_speed+200, 0, 5, 40);
  GRun(serv, GVolumeIndex(serv,cur_volume), 0, 0);
  cur_speed = 0;
  XFillRectangle( dpy, active_win, brighttext, 
		 cur_speed+200, 0, 5, 40);
  sprintf(cur_speed_string,"%d",cur_speed);
  show_direct( speed_win);
  GSetState((int)DEBUG,0,SYNC_MODE);
}

do_control(event)
     XEvent event;
{
  extern int cur_volume, cur_mask;
  extern Display *dpy;
  extern Cursor lightcursor, leftcursor, rightcursor, loadcursor, 
  searchcursor, playcursor, audiocursor, indexcursor, stopcursor;
  extern Server *serv;
  extern int frame;
  extern Window root, speed_win, shuttlebar;
  extern XContext which_context;
  extern char frameinput[], speedinput[], cur_speed_string[];
  int *which;
  static index_state = TURN_OFF, audio_one_state = TURN_ON, 
  audio_two_state = TURN_ON;
  char *data;

  XFindContext( dpy, event.xany.window, which_context, (caddr_t *)&which);

  switch(*which) {
  case 0:
    XDefineCursor(dpy, root, leftcursor);
    XFlush(dpy);
    GJog(serv, GVolumeIndex(serv,cur_volume), REVERSE, cur_mask);
    GRelease(serv);
    XFillRectangle( dpy, shuttlebar, black_gc,
		   atoi(cur_speed_string) + 200, 0, 5, 40);
    XFillRectangle( dpy, shuttlebar, brighttext,
		   200, 0, 5, 40);
    XDefineCursor(dpy, root, lightcursor);
    strcpy(cur_speed_string, "0");
    show_direct( speed_win);
    break;
  case 1:
    XDefineCursor(dpy, root, stopcursor);
    XFlush(dpy);
    GRun(serv, GVolumeIndex(serv,cur_volume), 0, cur_mask);
    GRelease(serv);
    XFillRectangle( dpy, shuttlebar, black_gc,
		   atoi(cur_speed_string) + 200, 0, 5, 40);
    XFillRectangle( dpy, shuttlebar, brighttext,
		   200, 0, 5, 40);
    XDefineCursor(dpy, root, lightcursor);
    strcpy(cur_speed_string, "0");
    show_direct( speed_win);
    break;
  case 2:
    XDefineCursor(dpy, root, rightcursor);
    XFlush(dpy);
    GJog(serv, GVolumeIndex(serv,cur_volume), FORWARD, cur_mask);
    GRelease(serv);
    XFillRectangle( dpy, shuttlebar, black_gc,
		   atoi(cur_speed_string) + 200, 0, 5, 40);
    XFillRectangle( dpy, shuttlebar, brighttext,
		   200, 0, 5, 40);
    XDefineCursor(dpy, root, lightcursor);
    strcpy(cur_speed_string, "0");
    show_direct( speed_win);
    break;
  case 3:
    XDefineCursor(dpy, root, playcursor);
    XFlush(dpy);
    cur_speed = GRun(serv, GVolumeIndex(serv,cur_volume), 
		     atoi(speedinput), cur_mask);
    GRelease(serv);
    XFillRectangle( dpy, shuttlebar, black_gc,
		   atoi(cur_speed_string) + 200, 0, 5, 40);
    XDefineCursor(dpy, root, lightcursor);
    if (cur_speed > NO_ERROR)
	cur_speed = 0;
    sprintf(cur_speed_string,"%d",cur_speed);
    XFillRectangle( dpy, shuttlebar, brighttext,
		   atoi(cur_speed_string) + 200, 0, 5, 40);
    show_direct( speed_win);
    break;
  case 4:
    XDefineCursor(dpy, root, indexcursor);
    XFlush(dpy);
    if (index_state == TURN_ON) index_state = TURN_OFF;
    else index_state = TURN_ON;
    GConfigure(serv, GVolumeIndex(serv,cur_volume), INDEX_CTRL, index_state);
    GRelease(serv);
    XDefineCursor(dpy, root, lightcursor);
    break;
  case 5:
    frame = atoi(frameinput);
    XDefineCursor(dpy, root, searchcursor);
    XFlush(dpy);
    GSearch(serv, GVolumeIndex(serv,cur_volume), frame, cur_mask);
    GRelease(serv);
    XFillRectangle( dpy, shuttlebar, black_gc,
		   atoi(cur_speed_string) + 200, 0, 5, 40);
    XFillRectangle( dpy, shuttlebar, brighttext,
		   200, 0, 5, 40);
    XDefineCursor(dpy, root, lightcursor);
    strcpy(cur_speed_string, "0");
    show_direct( speed_win);
    break;
  case 6:
    XUnmapWindow(dpy, root);
    XSync(dpy, 0);
    exit(0);
    break;
  case 7:
    XDefineCursor(dpy, root, loadcursor);
    XFlush(dpy);
/*    GLoad(serv, GVolumeIndex(serv,cur_volume), LOAD);
    GRelease(serv);*/
    GRebuild(serv);
    error_handler(serv, VOLUME_LIST_OLD);
    XDefineCursor(dpy, root, lightcursor);
    strcpy(cur_speed_string, "0");
    show_direct( speed_win);
    break;
  case 8:
    XDefineCursor(dpy, root, loadcursor);
    XFlush(dpy);
    GLoad(serv, GVolumeIndex(serv,cur_volume), EJECT);
    error_handler(serv, VOLUME_LIST_OLD);
/*    GRelease(serv);*/
    XDefineCursor(dpy, root, lightcursor);
    strcpy(cur_speed_string, "0");
    show_direct( speed_win);
    break;
  case 9:
    XDefineCursor(dpy, root, audiocursor);
    XFlush(dpy);
    if (audio_one_state == TURN_ON) audio_one_state = TURN_OFF;
    else audio_one_state = TURN_ON;
    GConfigure(serv, GVolumeIndex(serv,cur_volume),LEFT_CHAN, audio_one_state);
    GRelease(serv);
    XDefineCursor(dpy, root, lightcursor);
    break;
  case 10:
    XDefineCursor(dpy, root, audiocursor);
    XFlush(dpy);
    if (audio_two_state == TURN_ON) audio_two_state = TURN_OFF;
    else audio_two_state = TURN_ON;
    GConfigure(serv, GVolumeIndex(serv,cur_volume),RIGHT_CHAN,audio_two_state);
    GRelease(serv);
    XDefineCursor(dpy, root, lightcursor);
    break;
  }
}
