home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / tc123.zip / tchelper.c < prev    next >
C/C++ Source or Header  |  1999-04-03  |  6KB  |  146 lines

  1. // tc123 helper applet by Death Syndrome, with help from dink...
  2. // compile using gcc -Zcrtdll -s -o tc123helper.exe tc123helper.c
  3. #define INCL_DOSFILEMGR       /* DOS File Manager values */
  4. #define INCL_DOSNMPIPES       /* DOS Named Pipes values */
  5. #define INCL_DOSSEMAPHORES    /* DOS Semaphore values */
  6. #define INCL_DOSERRORS        /* DOS Error values */
  7. #include <os2.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. const char tc_version[] = "1.0.1-RELEASE";                 // Program version
  13.  
  14. struct rawinf {
  15.     char fname[256], songinfo[256];
  16.     char track[32], artist[32], album[32], comment[32];
  17.     char year[6], playtime[10], timenow[10], genre[25];
  18. };
  19.  
  20. int main(int argc, char *argv[]) {
  21.  
  22.     char     outmessage[1024] = "";  // Write buffer
  23.     char     inmessage[1024]  = "";  // Read buffer
  24.     char     tempbuf1[1024]   = "";  // Temp area for building output
  25.     char     tempbuf2[1024]   = "";  // Temp area for building output
  26.     char     pipename[10]     = "";  // name of pipe
  27.     struct   rawinf *inf      = (struct rawinf *) inmessage;
  28.     
  29.     if (argv[1]!=NULL) {
  30.         if (!strcmp(argv[1],"rawinfo")) strcpy(outmessage,"*rawinfo\n\0");
  31.         if (!strcmp(argv[1],"songinfo")) strcpy(outmessage,"*status info\n\0");
  32.         if (!strcmp(argv[1],"file")) strcpy(outmessage,"*status file\n\0");
  33.         if (!strcmp(argv[1],"tag")) strcpy(outmessage,"*status tag\n\0");
  34.         if (!strcmp(argv[1],"-v")) { showver(); return(0); }
  35.         if (!strcmp(argv[1],"-h") || !strcmp(argv[1],"-?")) {
  36.             showhelp();
  37.             return(0);
  38.         }
  39.     }
  40.  
  41.     if (!strlen(outmessage)) strcpy(outmessage,"*status tag\n\0");
  42.  
  43.     // printf("%s\n", argv[3]);
  44.  
  45.     if (argc>=3) {
  46.         if (!strcmp(argv[2], "zmp3")) {
  47.             strcpy(pipename, "\\PIPE\\ZMP3");
  48.             if (!strcmp(argv[1], "tag")) strcpy (outmessage,"*rawinfo\n\0");
  49.         } else strcpy(pipename, "\\PIPE\\PM123");
  50.     } else strcpy(pipename, "\\PIPE\\PM123");
  51.  
  52.     if (!strcmp(outmessage,"*rawinfo\n")) {
  53.         if (getpipeinfo(pipename,outmessage,inmessage)!=1) {
  54.             if (!strcmp(argv[1],"tag")) {
  55.                 if (!strlen(inf->track)) {
  56.                     sprintf(tempbuf1, "Z!: %s%s%s%s", inf->fname," (",inf->playtime,") \n\0");
  57.                     strcpy(inmessage, tempbuf1);
  58.                     printf("%s", inmessage);
  59.                 } else {
  60.             // printf("%s", boolean ? string1 : string2);
  61.                     sprintf(tempbuf1, "Z!: %s%s%s ", strlen(inf->artist) ? inf->artist : "", strlen(inf->artist) ? ": " : "", inf->track);
  62.                     sprintf(tempbuf2, "%s(%s", tempbuf1, strlen(inf->album) ? inf->album : "");
  63.                     sprintf(tempbuf1, "%s%s", tempbuf2, (strlen(inf->album) && strlen(inf->year)) ? ", " : "");
  64.                     sprintf(tempbuf2, "%s%s", tempbuf1, strcmp(inf->year, "????") ? inf->year : "");
  65.                     sprintf(tempbuf1, "%s%s", tempbuf2, strcmp(inf->year, "????") ? ", " : "");
  66.                     sprintf(tempbuf2, "%s%s)\n\0", tempbuf1, inf->playtime);
  67.                     strcpy(inmessage, tempbuf2);
  68.                     printf("%s", inmessage);
  69.                     // printf("Z!: %s: %s (%s, %s, %s)\n", inf->artist, inf->track, inf->album, inf->year, inf->playtime);
  70.                 }
  71.             } else {
  72.                 printf("filename [%s]\n", inf->fname);
  73.                 printf("songinfo [%s]\n", inf->songinfo);
  74.                 printf("track    [%s]\n", inf->track);
  75.                 printf("artist   [%s]\n", inf->artist);
  76.                 printf("album    [%s]\n", inf->album);
  77.                 printf("comment  [%s]\n", inf->comment);
  78.                 printf("year     [%s]\n", inf->year);
  79.                 printf("playtime [%s]\n", inf->playtime);
  80.                 printf("timenow  [%s]\n", inf->timenow);
  81.                 printf("genre    [%s]\n", inf->genre);
  82.             }
  83.         }
  84.     } else {
  85.         if (getpipeinfo(pipename,outmessage,inmessage)==1) {
  86.             strcpy(inmessage,"0");
  87.             strcpy(outmessage,"*status file\n\0");
  88.             getpipeinfo(pipename,outmessage,inmessage);
  89.         }
  90.         printf("%s\n", inmessage);
  91.     }
  92.  
  93.     return NO_ERROR;
  94. }
  95.  
  96. int showver() {
  97.     printf("tc123helper applet for OS/2 version %s\n",tc_version);
  98.     printf("'May the farce be with you, momo!'\n");
  99. }
  100.  
  101. int showhelp() {
  102.     showver();
  103.  
  104.     printf("-h or -? :  This help screen.\n");
  105.     printf("songinfo :  Statistics on currently playing song.\n");
  106.     printf("file     :  Current filename being played.\n");
  107.     printf("tag      :  Tag info for current mp3.. gives file if no tag.\n");
  108.     printf("-v       :  Gives version number of this tc123 helper applet.\n");
  109. }
  110.  
  111. int getpipeinfo(char *pipe, char *outmessage, char *inmessage) {
  112.     APIRET   rc                     = NO_ERROR;   /* Return code */
  113.     HFILE    PipeHandle             = NULLHANDLE; /* Pipe handle */
  114.     ULONG    bytes                  = 0;
  115.     ULONG    Action                 = 0;
  116.     int      i;
  117.  
  118.     // open the player pipe, try 10 times before giving up!
  119.     for(i=1;i<=10;i++){
  120.         rc = DosOpen(pipe, &PipeHandle, &Action, 0, 0, FILE_OPEN,
  121.                      OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYREADWRITE |
  122.                      OPEN_FLAGS_FAIL_ON_ERROR, NULL);
  123.         if (rc == NO_ERROR) break; else DosSleep(100);
  124.     }
  125.  
  126.     if (rc != NO_ERROR) {
  127.        printf("DosOpen error: error code = %u\n", rc);
  128.        return 1;
  129.     }
  130.  
  131.     rc = DosWrite(PipeHandle, outmessage, strlen(outmessage), &bytes);
  132.     if (rc != NO_ERROR) {
  133.        printf("DosWrite error: error code = %u\n", rc);
  134.        return 1;
  135.     }
  136.  
  137.     rc = DosRead(PipeHandle, inmessage, 1024, &bytes);
  138.     if (rc != NO_ERROR) {
  139.        printf("DosRead error: error code = %u\n", rc);
  140.        return 1;
  141.     }
  142.  
  143.     rc = DosClose(PipeHandle);
  144.     return bytes;
  145. }
  146.