home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Viewers / VideoStreamV1.0 / Source / Controller.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  3.6 KB  |  166 lines

  1. /*
  2.  * Copyright 1994, Black Adder Research, Inc.  This source code may be
  3.  * redistributed and modified with one restriction - do not claim that 
  4.  * you wrote it.
  5.  *
  6.  * Black Adder Research, Inc.
  7.  * 730 Norell Ave. North
  8.  * Stillwater, MN  55082
  9.  * 
  10.  */
  11.  
  12. #import <appkit/appkit.h>
  13. #import <math.h>
  14. #import <strings.h>
  15. #import <defaults/defaults.h>
  16. #import "Controller.h"
  17. #import "AVIView.h"
  18. #import "MPEGView.h"
  19. #import <sys/param.h>
  20. #import <libc.h>
  21.  
  22. /* File extensions for video files */
  23. const char *aviType = "avi";
  24. const char *mpgType = "mpg";
  25.  
  26.  
  27. @implementation Controller : Object
  28.  
  29.  
  30. - openRequest:sender
  31. {
  32.     id        openReq;
  33.     char    srcName[256];
  34.     const char    *const fileTypes[] = {aviType, mpgType, NULL};  
  35.     
  36.     openReq = [OpenPanel new];
  37.     if ([openReq runModalForTypes:fileTypes]) {
  38.         strcpy(srcName,  [openReq filename]);
  39.         [self openFile: srcName];
  40.     }
  41.     return self;
  42. }
  43.  
  44.  
  45. - (BOOL) openFile: (char *) srcName
  46. {
  47.     id  window, view=nil;
  48.     NXRect    rect;
  49.     char *ext;
  50.     BOOL retVal = NO;
  51.     
  52.     /* determine file type and allocate appropriate view type */
  53.     ext = rindex(srcName, '.');
  54.     ++ext;
  55.     if(0 == strncmp(aviType, ext, 3)) {
  56.         if([self getAVIFrameSize: &(rect.size) forFile: srcName]) {
  57.             view = [[AVIView alloc] initSize: (const NXSize *) &(rect.size)];
  58.         }
  59.     }
  60.     else {
  61.         if([self getMPEGFrameSize: &(rect.size) forFile: srcName]) {
  62.             view = [[MPEGView alloc] initSize: (const NXSize *) &(rect.size)];
  63.         }
  64.     }
  65.     
  66.     if (nil != view) {
  67.         rect.origin.x = 0.0;
  68.         rect.origin.y = 0.0;
  69.         window = [[Window alloc] initContent:&rect
  70.                 style:NX_TITLEDSTYLE
  71.                 backing:NX_NONRETAINED
  72.                 buttonMask:NX_CLOSEBUTTONMASK
  73.                 defer:NO];
  74.         [window setDelegate:self];
  75.         [window setHideOnDeactivate: YES];
  76.         [[window setContentView:view] free];
  77.         [window center];
  78.         [window setTitleAsFilename:srcName];
  79.         [window makeKeyAndOrderFront:self];
  80.         [window display];
  81.         [view runFromFile: srcName];
  82.         retVal = YES;
  83.     }
  84.     return(retVal);
  85. }
  86.  
  87.  
  88. - getAVIFrameSize: (NXSize *) fSize forFile: (char *) aviFile
  89. {
  90.     char command[256];
  91.     id retVal = nil;
  92.     int iTemp;
  93.     FILE *ifp;
  94.  
  95.     if (strcmp(aviFile, "") != 0) {
  96.         sprintf(command, "%s/aviDecode %s", [[NXBundle mainBundle] directory], aviFile);
  97.         if ((ifp = popen(command, "r")) != NULL) {
  98.             /* width and height are first 8 bytes (2 ints) in stream */
  99.             fread(&iTemp, 1, sizeof(int), ifp);
  100.             fSize->width = (float) iTemp;
  101.             fread(&iTemp, 1, sizeof(int), ifp);
  102.             fSize->height = (float) iTemp;
  103.             pclose(ifp);
  104.             retVal = self;
  105.         }
  106.     }
  107.     return(retVal);
  108. }
  109.  
  110.  
  111. - getMPEGFrameSize: (NXSize *) fSize forFile: (char *) mpegFile
  112. {
  113.     char command[256];
  114.     id retVal = nil;
  115.     int iTemp;
  116.     FILE *ifp;
  117.  
  118.     if (strcmp(mpegFile, "") != 0) {
  119.         sprintf(command, "%s/mpegDecode %s", [[NXBundle mainBundle] directory], mpegFile);
  120.         if ((ifp = popen(command, "r")) != NULL) {
  121.             /* skip over frame number */
  122.             fread(&iTemp, 1, sizeof(int), ifp);
  123.             /* now get width and height */
  124.             fread(&iTemp, 1, sizeof(int), ifp);
  125.             fSize->width = (float) iTemp;
  126.             fread(&iTemp, 1, sizeof(int), ifp);
  127.             fSize->height = (float) iTemp;
  128.             pclose(ifp);
  129.             retVal = self;
  130.         }
  131.     }
  132.     return(retVal);
  133. }
  134.  
  135.  
  136. - playAgain: sender
  137. {
  138.     id theWindow = [NXApp mainWindow];
  139.     if([[theWindow contentView] respondsTo: @selector(runAgain)]) {
  140.         [[theWindow contentView] runAgain];
  141.     }
  142.     return(self);
  143. }
  144.  
  145.  
  146. /* We always want to accept files. */
  147. - (BOOL)appAcceptsAnotherFile:sender
  148. {
  149.     return (YES);
  150. }
  151.  
  152.  
  153. /* Open avi file from workspace. */
  154. - (int) appOpenFile:(const char *)filename type:(const char *)aType
  155. {
  156.     BOOL retVal = NO;
  157.  
  158.     if((!strcmp(aType, aviType)) || (!strcmp(aType, mpgType))) {
  159.         retVal = [self openFile:(char *)filename];
  160.     }
  161.     return (retVal);
  162. }
  163.  
  164.  
  165. @end
  166.