//#include <win.h-->
#pragma option w32
#jumptomain NONE

#include <winbase.h-->
#include <wingdi.h-->
#include <winuser.h-->
#include <shellapi.h-->
#include "winmm.fnc"
#include "cdtray.rc"

#define TNI_MOUSE 101
#define MI_ABOUT  2001
#define MI_EJECT  2002
#define MI_EXIT   2003
#define MI_CLOSE  2004

#define CD_FLGNONE  22
#define CD_OPEN  1000
#define CD_CLOSE 2000


HINSTANCE hInst;
char szProgName="CDTRAY";
char szProgCaption="CDTRAY";
HANDLE hIC_CDBUTTON;
NOTIFYICONDATA nid1;
HANDLE hPopMenu1;
int CDFLAG=CD_FLGNONE;
dword WM_KBDHOOK;

byte szTip1[64]="CD tray button v1.0";//"CD tray button v1.0";



int main()
{
 HWND     hWnd;
 MSG      lpMsg;
 WNDCLASS wcApp;


 hInst=GetModuleHandle(0);


 wcApp.lpszClassName=#szProgName;
 wcApp.hInstance=hInst;
 wcApp.lpfnWndProc=#WndProc;
 wcApp.hCursor=LoadCursor(0,IDC_ARROW);
 wcApp.hIcon=0;
 wcApp.lpszMenuName=0;
 wcApp.hbrBackground=GetStockObject(GRAY_BRUSH);
 wcApp.style=CS_HREDRAW+CS_VREDRAW;
 wcApp.cbClsExtra=0;
 wcApp.cbWndExtra=0;

 RegisterClass(#wcApp);


 hWnd=CreateWindowEx(0,#szProgName,#szProgCaption,
                   WS_OVERLAPPEDWINDOW,
                   CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
                   0,0,hInst,0);

 ShowWindow(hWnd,SW_HIDE);
 UpdateWindow(hWnd);

 InitTrayIcon(hWnd,hInst);

 CloseCDTray();
 CDFLAG=CD_CLOSE;

 WHILE (GetMessage(#lpMsg,0,0,0))
      {
       TranslateMessage(#lpMsg);
       DispatchMessage(#lpMsg);
      }
 return(lpMsg.wParam);
}

dword WndProc(HWND hWnd,UINT messg,WPARAM wpar, LPARAM lpar)
{
  IF (messg==WM_KBDHOOK)
 {
  OperateWithCD();
 }

 SWITCH (messg)
 {
  CASE WM_COMMAND:
   SWITCH (wpar)
   {
    CASE MI_EXIT:
    DoneTrayIcon();
    PostQuitMessage(0);
    BREAK;

    CASE MI_EJECT:
    OpenCDTray();
    BREAK;

    CASE MI_CLOSE:
    CloseCDTray();
    BREAK;

    CASE MI_ABOUT:
    ShowAboutDialog();
    BREAK;
   }
  BREAK;



  CASE WM_DESTROY:
  PostQuitMessage(0);
  BREAK;


  CASE TNI_MOUSE:
    IF (lpar==WM_RBUTTONDOWN) ShowPopMenu(hWnd);
    IF (lpar==WM_LBUTTONDOWN) OperateWithCD();
  BREAK;


  default:
   RETURN(DefWindowProc(hWnd,messg,wpar,lpar));
 }

 return(0);
}

void InitTrayIcon(HANDLE hwnd, HANDLE hInst)
{
 int cnt;

 hIC_CDBUTTON=LoadIcon(hInst,"CDBUTTON");
 nid1.cbSize=sizeof(nid1);
 nid1.hWnd=hwnd;
 nid1.uID=1000;
 nid1.uFlags=NIF_ICON+NIF_MESSAGE+NIF_TIP;
 nid1.uCallbackMessage=TNI_MOUSE;
 nid1.hIcon=hIC_CDBUTTON;
// nid1.szTip=szTip1;
 FOR (cnt=0; cnt<64;cnt++)
 {
  nid1.szTip[cnt]=szTip1[cnt];
 }
// StrCpy(nid1.szTip,szTip);
 Shell_NotifyIcon(NIM_ADD,#nid1);
}

void DoneTrayIcon()
{
 Shell_NotifyIcon(NIM_DELETE,#nid1);
}

void OperateWithCD()
{
 char szCDStatus[10];
 mciSendString("status cdaudio media present wait",#szCDStatus,10,0);
 IF (szCDStatus[0]=='t')
    {
     mciSendString("set cdaudio door open wait",0,0,0);
     CDFLAG=CD_OPEN;
    }
 IF (szCDStatus[0]=='f')
    {
     IF (CDFLAG==CD_OPEN)
         {
          mciSendString("set cdaudio door closed wait",0,0,0);
          CDFLAG=CD_CLOSE;
         } ELSE
         {
          mciSendString("set cdaudio door open wait",0,0,0);
          CDFLAG=CD_OPEN;
         }

    }
}

void OpenCDTray()
{
 mciSendString("set cdaudio door open wait",0,0,0);
 CDFLAG=CD_OPEN;
}

void CloseCDTray()
{
 mciSendString("set cdaudio door closed wait",0,0,0);
 CDFLAG=CD_OPEN;
}

/*void OperateWithMenu()
{
}
	*/
void ShowPopMenu(HANDLE hwnd)
{
 POINT curspos;
// int xsize,ysize;

 hPopMenu1=CreatePopupMenu();
 AppendMenu(hPopMenu1,MF_ENABLED,MI_ABOUT,"About");
 AppendMenu(hPopMenu1,MF_ENABLED,MI_EJECT,"Eject CD door");
 AppendMenu(hPopMenu1,MF_ENABLED,MI_CLOSE,"Close CD door");
 AppendMenu(hPopMenu1,MF_SEPARATOR,0,"");
 AppendMenu(hPopMenu1,MF_ENABLED,MI_EXIT,"E&xit");

// xsize=GetSystemMetrics(SM_CXSCREEN);
// ysize=GetSystemMetrics(SM_CYSCREEN);
 GetCursorPos(#curspos);
 ClientToScreen(GetDesktopWindow(),#curspos);

 TrackPopupMenu(hPopMenu1,0,curspos.x,curspos.y,0,hwnd,0);


 DestroyMenu(hPopMenu1);
}

void ShowAboutDialog()
{
 //ShellAbout(0,"<Easy CD tray button v1.0!>","<CDTRAY.EXE (c)2001 Gordeychik Vladimir!>",hIC_CDBUTTON);
 MessageBox(0,"Written by Gordeychik Volodya (c)2001.","About CD tray button v1.0",MB_OK);
}
