home *** CD-ROM | disk | FTP | other *** search
/ Lion Share / lionsharecd.iso / utils_mz / v10n09.zip / WINSAVER.C < prev    next >
C/C++ Source or Header  |  1991-02-11  |  18KB  |  603 lines

  1. /* winsaver.c RHS 1/25/91
  2.  */
  3.  
  4. #include<windows.h>
  5. #include<stdio.h>
  6. #include<mem.h>
  7. #include<string.h>
  8. #include<stdlib.h>
  9.  
  10. #define MAXWINDOWSPARM  256
  11. #define MAXFILENAME     80
  12. #define MAXWINS         256
  13. #define ICONIC          SW_SHOWMINNOACTIVE
  14.  
  15. #define SC_USER         (0xF000-1)
  16. #define SC_ABOUT        SC_USER
  17. #define SC_AUTOSAVE     SC_USER-1
  18. #define SC_AUTOLOAD     SC_USER-2
  19.  
  20. #define MYTIMER         1
  21. #define MYTIMER2        2
  22.  
  23. typedef struct _windows
  24.     {
  25.     RECT rect;
  26.     int winState;
  27.     HWND hWnd;
  28.     char filename[MAXFILENAME];
  29.     } Wins;
  30. Wins windows[MAXWINS];
  31.  
  32. long FAR PASCAL WndProc(HWND, WORD, WORD, LONG);
  33.  
  34. void SaveSession(void);
  35. void RestoreSession(void);
  36. void GetWindowList(void);
  37. void PaintWindowList(void);
  38. int  ReadProfile(void);
  39. void LoadPrograms(int maxwins);
  40. void RestoreSession(void);
  41. void Execute(char *command, int ncmdshow);
  42. HWND GetWindowHandle(char *name);
  43. BOOL HandleUsed(HWND hWnd);
  44. void ActivateProgMan(void);
  45. void UpdateSystemMenu(void);
  46. void Autoload(void);
  47.  
  48. int getint(char **p);
  49. BOOL bufmatch(char *str, char *winbuf, char *fullname);
  50.  
  51. static char szCopyRight[] = " WinSaver, Copyright (C) Richard Hale Shaw, 1991 ";
  52. static char szWinSaver[] = "WinSaver";
  53. static char szAutoSave[] = "Autosave";
  54. static char szAutoLoad[] = "Autoload";
  55.  
  56. char szAppName[MAXFILENAME];
  57.  
  58. BOOL AutoSave = FALSE;
  59. BOOL AutoLoad = FALSE;
  60.  
  61. #define DEFAULTTIMER    1000
  62. WORD TimerVal = DEFAULTTIMER;
  63.  
  64. HCURSOR hNormalCursor, hHourGlassCursor;
  65. HWND WinSaver;
  66. HANDLE hPrev;
  67. LPSTR CmdLine;
  68.  
  69. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  70.     LPSTR lpCmdLine, int nCmdShow)
  71.     {
  72.     MSG msg;
  73.     WNDCLASS wndclass;
  74.  
  75.     hPrev = hPrevInstance;
  76.  
  77.     if(!hPrevInstance)    
  78.         {
  79.         wndclass.style = NULL;
  80.         wndclass.lpfnWndProc = WndProc;
  81.         wndclass.cbClsExtra = 0;       
  82.         wndclass.cbWndExtra = 0;       
  83.         wndclass.hInstance = hInstance;
  84.         wndclass.hIcon = LoadIcon(hInstance, "WINSAVER");
  85.         wndclass.hCursor = NULL;
  86.         wndclass.hbrBackground = GetStockObject(WHITE_BRUSH); 
  87.         wndclass.lpszMenuName =  NULL;
  88.         wndclass.lpszClassName = szWinSaver;
  89.  
  90.         RegisterClass(&wndclass);
  91.         }
  92.  
  93.     hNormalCursor = LoadCursor(NULL, IDC_ARROW);
  94.     hHourGlassCursor = LoadCursor(NULL, IDC_WAIT);
  95.  
  96.     WinSaver = CreateWindow(szWinSaver,
  97.         "Session Saver",
  98.         WS_OVERLAPPEDWINDOW,            
  99. //        0,
  100.         CW_USEDEFAULT,                  
  101.         CW_USEDEFAULT,                  
  102.         CW_USEDEFAULT,                  
  103.         CW_USEDEFAULT,                  
  104.         NULL,                           
  105.         NULL,                           
  106.         hInstance,                      
  107.         NULL);
  108.  
  109.     UpdateSystemMenu();
  110.  
  111.     ShowWindow(WinSaver, SW_SHOWMINIMIZED);
  112.     UpdateWindow(WinSaver);        
  113.  
  114.     while(GetMessage(&msg, NULL, 0, 0))
  115.         {
  116.         TranslateMessage(&msg);
  117.         DispatchMessage(&msg); 
  118.         }
  119.     return (msg.wParam);  
  120.     }
  121.  
  122.  
  123.  
  124. long FAR PASCAL WndProc(HWND hWnd, WORD message,
  125.     WORD wParam, LONG lParam)
  126.     {
  127.     static BOOL done1 = FALSE;
  128.     static BOOL done2 = FALSE;
  129.  
  130.     switch(message)
  131.         {
  132.         case WM_CREATE:
  133.             {
  134.             WORD w = LOBYTE(GetVersion());
  135.  
  136.             if(w < 3)                           // if Win 1.0 or Win 2.0
  137.                 {
  138.                 MessageBox(hWnd,
  139.                     "Sorry, WinSaver must run on Windows 3.x or higher!",
  140.                     szWinSaver,
  141.                     MB_ICONEXCLAMATION);
  142.                 SendMessage(hWnd,WM_DESTROY,0,0L);
  143.                 }
  144.             else if(hPrev)                      // if a previous instance
  145.                 {
  146.                 MessageBox(hWnd,
  147.                     "WinSaver already running!",
  148.                     szWinSaver,
  149.                     MB_ICONEXCLAMATION);
  150.                 SendMessage(hWnd,WM_DESTROY,0,0L);
  151.                 }
  152.             else
  153.                 SetTimer(hWnd,MYTIMER,50,NULL);
  154.             break;
  155.             }
  156.  
  157.         case WM_INITMENU:           // modify the system menu
  158.             CheckMenuItem(GetSystemMenu(WinSaver,0),
  159.                 SC_AUTOSAVE,
  160.                 (MF_BYCOMMAND | (AutoSave ? MF_CHECKED : MF_UNCHECKED)));
  161.             CheckMenuItem(GetSystemMenu(WinSaver,0),
  162.                 SC_AUTOLOAD,
  163.                 (MF_BYCOMMAND | (AutoLoad ? MF_CHECKED : MF_UNCHECKED)));
  164.             break;
  165.  
  166.         case WM_TIMER:
  167.             {
  168.             if(!done1 && wParam == MYTIMER)
  169.                 {
  170.                 SetCursor(hHourGlassCursor);
  171.                 KillTimer(hWnd,MYTIMER);
  172.                 RestoreSession();
  173.                 GetModuleFileName(GetClassWord(hWnd,GCW_HMODULE),
  174.                         szAppName, MAXFILENAME-1);
  175.                 SetTimer(hWnd,MYTIMER2,TimerVal,NULL);
  176.                 done1 = TRUE;
  177.                 }
  178.             if(!done2 && wParam == MYTIMER2)
  179.                 {
  180.                 char buf[20];
  181.  
  182.                 KillTimer(hWnd,MYTIMER2);
  183.                 SetCursor(hNormalCursor);
  184.                 done2 = TRUE;
  185.                 }
  186.             }
  187.             break;
  188.  
  189.         case WM_QUERYENDSESSION:
  190.             if(AutoSave || (MessageBox(hWnd,"Save this session?",szWinSaver,
  191.                 MB_ICONQUESTION | MB_YESNO) == IDYES))
  192.                 {
  193.                 SetCursor(hHourGlassCursor);
  194.                 GetWindowList();
  195.                 SaveSession();
  196.                 }
  197.             return -1L;
  198.  
  199.         case WM_QUERYOPEN:                      // prevent icon from opening
  200.             return 0L;                          // into a window
  201.  
  202.         case WM_CLOSE:
  203.             DestroyWindow(hWnd);
  204.             break;
  205.  
  206.         case WM_DESTROY:
  207.             PostQuitMessage(0);
  208.             break;
  209.  
  210.         case WM_SYSCOMMAND:
  211.             switch(wParam)
  212.                 {
  213.                 case SC_ABOUT:
  214.                     MessageBox(hWnd,szCopyRight,szWinSaver,
  215.                         MB_ICONINFORMATION);
  216.                     break;
  217.  
  218.                 case SC_AUTOLOAD:
  219.                     Autoload();
  220.                     break;
  221.  
  222.                 case SC_AUTOSAVE:
  223.                     AutoSave = !AutoSave;
  224.                     CheckMenuItem(GetSystemMenu(WinSaver,0),
  225.                         SC_AUTOSAVE,(MF_BYCOMMAND |
  226.                             (AutoSave ? MF_CHECKED : MF_UNCHECKED)));
  227.                     break;
  228.                 default:
  229.                     goto defwindowproc;
  230.                 }
  231.             break;
  232.  
  233.          default:
  234. defwindowproc:
  235.             return DefWindowProc(hWnd, message, wParam, lParam);
  236.         }
  237.     return 0L;
  238.     }
  239.  
  240.  
  241. void UpdateSystemMenu(void)
  242.     {
  243.     HMENU hSysMenu = GetSystemMenu(WinSaver,0);
  244.  
  245.     RemoveMenu(hSysMenu, SC_RESTORE, MF_BYCOMMAND);
  246.     RemoveMenu(hSysMenu, SC_MINIMIZE, MF_BYCOMMAND);
  247.     RemoveMenu(hSysMenu, SC_MAXIMIZE, MF_BYCOMMAND);
  248.     RemoveMenu(hSysMenu, SC_SIZE, MF_BYCOMMAND);
  249.  
  250.     AppendMenu(hSysMenu, MF_SEPARATOR, 0, NULL);
  251.     AppendMenu(hSysMenu, MF_STRING, SC_ABOUT, "About...");
  252.     AppendMenu(hSysMenu, MF_STRING, SC_AUTOSAVE, "Autosave");
  253.     AppendMenu(hSysMenu, MF_STRING, SC_AUTOLOAD, "Autoload");
  254.     }
  255.  
  256. char outbuf[MAXWINDOWSPARM];
  257.  
  258. void Autoload(void)
  259.     {
  260.     char *p;
  261.  
  262.     GetProfileString("windows","load",NULL,outbuf,sizeof(outbuf));
  263.     strupr(outbuf);
  264.  
  265.     AutoLoad = !AutoLoad;
  266.  
  267.         // if Auto Load is selected and app name is not in LOAD= list
  268.     if(AutoLoad && !strstr(outbuf,szAppName))
  269.         {
  270.         strcat(outbuf," ");                     // add blank and name to 
  271.         strcat(outbuf,szAppName);               // LOAD= list
  272.         WriteProfileString("windows","load",outbuf);
  273.         }
  274.         // if Auto Load is not selected and app name is in LOAD= list
  275.     if(!AutoLoad && (p = strstr(outbuf,szAppName)))
  276.         {
  277.         *p = '\0';                              // NULL at start of appname
  278.         p += strlen(szAppName);                 // move past appname
  279.         strcat(outbuf,p);                       // copy everybody up
  280.         WriteProfileString("windows","load",outbuf);
  281.         }
  282.     }
  283.  
  284. void SaveSession(void)
  285.     {
  286.     int i, j;
  287.     char buf[10];
  288.     char *p;
  289.     RECT rect;
  290.     int winState;
  291.  
  292.     WriteProfileString(szWinSaver,NULL,NULL);
  293.     WriteProfileString(szWinSaver,szAutoSave,(AutoSave ? "1" : "0"));
  294.     WriteProfileString(szWinSaver,szAutoLoad,(AutoLoad ? "1" : "0"));
  295.  
  296.     for(i = 0; i < MAXWINS; i++)
  297.         if(!windows[i+1].hWnd)
  298.             break;
  299.  
  300.     for(j = 0; i >= 0; i--)
  301.         {
  302.         p = windows[i].filename;
  303.  
  304.             // eliminate all entries that are NULL, blank,
  305.             // or contain "\\USER.EXE"
  306.         if(!(*p) ||
  307.             (*p == ' ') ||
  308.             strstr(p,"\\USER.EXE") ||
  309.             strstr(p, "\\PROGMAN.EXE"))
  310.             continue;
  311.  
  312.         sprintf(buf,"Win%d",j++);
  313.  
  314.         GetWindowRect(windows[i].hWnd,&rect);
  315.  
  316.         winState = SW_SHOWNORMAL;
  317.         if(IsIconic(windows[i].hWnd))
  318.             winState = ICONIC;
  319. //            winState = SW_SHOWMINIMIZED;
  320.         if(IsZoomed(windows[i].hWnd))
  321.             winState = SW_SHOWMAXIMIZED;
  322.  
  323.         sprintf(outbuf,"%s %d %d %d %d %d",
  324.             windows[i].filename,
  325.             winState,
  326.             rect.left, rect.top, rect.right, rect.bottom);
  327.  
  328.         WriteProfileString(szWinSaver,buf,outbuf);
  329.         }
  330.     }
  331.  
  332. void GetWindowList(void)
  333.     {
  334.     int i;
  335.  
  336.     memset(windows,0,sizeof(windows));
  337.  
  338.     windows[0].hWnd = GetWindow(GetDesktopWindow(),GW_CHILD);
  339.     for(i = 0; i < MAXWINS; i++)
  340.         {
  341.         GetModuleFileName(GetClassWord(windows[i].hWnd,GCW_HMODULE),
  342.             windows[i].filename, MAXFILENAME-1);
  343.         if(!(windows[i+1].hWnd = GetNextWindow(windows[i].hWnd,GW_HWNDNEXT)))
  344.             break;
  345.         }
  346.     }
  347.  
  348. void PaintWindowList(void)
  349.     {
  350.     int i;
  351.     PAINTSTRUCT ps;
  352.     RECT rect;
  353.  
  354.     BeginPaint(WinSaver, &ps);
  355.     GetClientRect(WinSaver,&rect);
  356.  
  357.     for(i = 0; windows[i].hWnd && i < MAXWINS; i++)
  358.         {
  359.         sprintf(outbuf,"HWND: %u %s state=%d @ %d %d %d %d",
  360.             windows[i].hWnd,
  361.             windows[i].filename,
  362.             windows[i].winState,
  363.             windows[i].rect.left,
  364.             windows[i].rect.top,
  365.             windows[i].rect.right,
  366.             windows[i].rect.bottom);
  367.  
  368.         TextOut(ps.hdc,0,i*15,outbuf,strlen(outbuf));
  369.         }        
  370.  
  371.     EndPaint(WinSaver, &ps);
  372.     }
  373.  
  374. int getint(char **p)
  375.     {
  376.     char *q = *p, *r;
  377.  
  378.     r = strchr(q,' ');
  379.     *r++ = '\0';
  380.     *p = r;
  381.     return atoi(q);
  382.     }
  383.  
  384. void RestoreSession(void)
  385.     {
  386.     int maxwins;
  387.  
  388.     maxwins = ReadProfile();
  389.     LoadPrograms(maxwins);
  390. //    ActivateProgMan();
  391.     }
  392.  
  393. void ActivateProgMan(void)
  394.     {
  395.     HWND hWnd;
  396.     char progmanfile[MAXFILENAME];
  397.  
  398.     GetWindowsDirectory(progmanfile,MAXFILENAME-1);
  399.     strcat(progmanfile,"\\PROGMAN.EXE");
  400.     hWnd = GetWindowHandle(progmanfile);
  401.     SetActiveWindow(hWnd);
  402.     }
  403.  
  404. BOOL HandleUsed(HWND hWnd)
  405.     {
  406.     int i;
  407.  
  408.     for(i = 0; i < MAXWINS; i++)
  409.         if(hWnd == windows[i].hWnd)
  410.             return TRUE;
  411.     return FALSE;
  412.     }
  413.  
  414. HWND GetWindowHandle(char *name)
  415.     {
  416.     char filename[MAXFILENAME];
  417.     HWND hWnd;
  418.     int i;
  419.  
  420.     hWnd = GetWindow(GetDesktopWindow(),GW_CHILD);
  421.  
  422.     for(i = 0; i < MAXWINS; i++)
  423.         {
  424.         if(!HandleUsed(hWnd))                   // if handle is not in list
  425.             {                                   // get it's filename
  426.             GetModuleFileName(GetClassWord(hWnd,GCW_HMODULE),filename,
  427.                 MAXFILENAME-1);
  428.             if(!strcmp(name,filename))   // if filenames match
  429.                 break;
  430.             }
  431.         hWnd = GetNextWindow(hWnd,GW_HWNDNEXT);
  432.         }
  433.     return hWnd;
  434.     }
  435.  
  436. #define WM_BOGUS1   WM_USER+0x00A0
  437. #define WM_BOGUS2   WM_USER+0x00AA
  438.  
  439. void LoadPrograms(int maxwins)
  440.     {
  441.     int i;
  442.  
  443.     for(i = 0; i < maxwins && windows[i].hWnd == 0xffff; i++)
  444.         {
  445.         if(strstr(windows[i].filename,"\\WINSAVER.EXE"))
  446.             windows[i].hWnd = WinSaver;
  447.         else
  448.             {
  449.             Execute(windows[i].filename,SW_HIDE);
  450.             windows[i].hWnd = GetWindowHandle(windows[i].filename);
  451. //            SendMessage(windows[i].hWnd,WM_BOGUS1,0xffff,0xffffffff);
  452.             }
  453.         }
  454.     for(i = 0; i < maxwins && windows[i].hWnd != 0xffff; i++)
  455.         {
  456.         if(windows[i].winState != ICONIC)
  457.             MoveWindow(windows[i].hWnd,
  458.                 windows[i].rect.left,
  459.                 windows[i].rect.top,
  460.                 windows[i].rect.right-windows[i].rect.left,
  461.                 windows[i].rect.bottom-windows[i].rect.top,
  462.                 TRUE);
  463.         ShowWindow(windows[i].hWnd,windows[i].winState);
  464. //        PostMessage(windows[i].hWnd,WM_BOGUS2,0xffff,0xffffffff);
  465.         }
  466.     TimerVal *= i;                              // multiply by # of windows
  467.     }
  468.  
  469.  
  470. BOOL bufmatch(char *str, char *winbuf, char *fullname)
  471.     {
  472.     char *p, *q;
  473.     char fname[MAXFILENAME], fname2[MAXFILENAME];
  474.  
  475.     p = winbuf;                                 // set to start of buffer
  476.     while((p = strstr(p,str)))                  // look for str in p
  477.         {                                       // if found,
  478.         p--;                                    // look at previous char
  479.                                                 // if p not one of these
  480.         if((*p != '=') && (*p != ',') && (*p != ' '))
  481.             {
  482.             p += 2;                             // move past match point
  483.             continue;                           // try again
  484.             }
  485.                                                 // see if directories match
  486.         strcpy(fname,fullname);
  487.         q = strrchr(fname,'\\');
  488.         *q = '\0';
  489.         GetWindowsDirectory(fname2,MAXFILENAME-1);
  490.         if(!strcmp(fname,fname2))               // if directories match,
  491.             return TRUE;                        // return TRUE
  492.         p += 2;                                 // move past match point
  493.         }
  494.     return FALSE;
  495.     }
  496.  
  497. char loadrun[MAXWINDOWSPARM*2],
  498. windir[MAXWINDOWSPARM];
  499. char inbuf[MAXWINDOWSPARM];
  500.  
  501. int ReadProfile(void)
  502.     {    
  503.     char buf[13], *p;
  504.     int i,j;
  505.  
  506.     GetProfileString("windows","load",NULL,loadrun,sizeof(loadrun));
  507.     p = &loadrun[strlen(loadrun)];
  508.     *p++ = ' ';
  509.     *p = '\0';
  510.     GetProfileString("windows","run",NULL,p,sizeof(loadrun)-strlen(loadrun));
  511.     strupr(loadrun);
  512.  
  513.     GetWindowsDirectory(windir,MAXWINDOWSPARM-1);
  514.  
  515.     GetProfileString(szWinSaver,szAutoSave,NULL,buf,sizeof(buf));
  516.     if(!strcmp(buf,"1"))
  517.         AutoSave = TRUE;
  518.  
  519.     GetProfileString(szWinSaver,szAutoLoad,NULL,buf,sizeof(buf));
  520.     if(!strcmp(buf,"1"))
  521.         AutoLoad = TRUE;
  522.  
  523.  
  524.     for(i = j = 0; i < MAXWINS; )
  525.         {
  526.         sprintf(buf,"Win%d",j++);
  527.         if(!GetProfileString(szWinSaver,buf,NULL,inbuf,MAXWINDOWSPARM))
  528.             break;
  529.         p = strchr(inbuf,' ');
  530.         *p++ = '\0';
  531.  
  532.         windows[i].winState = getint(&p);
  533.         windows[i].rect.left = getint(&p);
  534.         windows[i].rect.top = getint(&p);
  535.         windows[i].rect.right = getint(&p);
  536.         windows[i].rect.bottom = getint(&p);
  537.  
  538.         strcpy(windows[i].filename,inbuf);
  539.         if(strstr(loadrun,windows[i].filename)) // if fullname match
  540.             continue;
  541.  
  542.         p = strrchr(windows[i].filename,'\\');
  543.         strcpy(buf,++p);                        // p to beginning of filename
  544.  
  545.                                                 // if filename+ext match
  546.         if(bufmatch(buf,loadrun,windows[i].filename))
  547.             continue;
  548.  
  549.         p = strchr(buf,'.');
  550.         *p = '\0';
  551.                                                 // if filename match
  552.         if(bufmatch(buf,loadrun,windows[i].filename))
  553.             continue;
  554.  
  555.         windows[i++].hWnd = 0xffff;
  556.         }
  557.     return i;
  558.     }
  559.  
  560.  
  561. char *messages[5] =
  562.     {    
  563.     "Out of memory",
  564.     "(Unknown Error)",
  565.     "File not found",
  566.     "Path not found",
  567.     "Failed attempt to dynamically link"
  568.     };
  569.  
  570. void Execute(char *command, int ncmdshow)
  571.     {
  572.     char buf[256],*p;
  573.     int retval;
  574.  
  575.     strcpy(buf,command);                        // copy command to buffer
  576.     p = strrchr(buf,'\\');                      // find last '\'
  577.     if((p-buf) == 2)                            // if "C:\"
  578.         p++;                                    // point past '\'
  579.     *p = '\0';                                  // remove the character
  580.     setdisk(*buf-'A');                          // change to that disk
  581.     chdir(buf);                                 // change to that directory
  582.  
  583.     if((retval = WinExec(command,ncmdshow)) < 32)
  584.         {
  585.         strcpy(buf,"Unable to execute '");
  586.         strcat(buf,command);
  587.         strcat(buf,"', ");
  588.         strcat(buf,messages[retval]);
  589.         MessageBox(WinSaver,buf,"WinSaver Error!",MB_ICONEXCLAMATION);
  590.         }
  591.     strcpy(buf,szAppName);                      // copy command to buffer
  592.     p = strrchr(buf,'\\');                      // find last '\'
  593.     if((p-buf) == 2)                            // if "C:\"
  594.         p++;                                    // point past '\'
  595.     *p = '\0';                                  // remove the character
  596.     setdisk(*buf-'A');                          // change to that disk
  597.     chdir(buf);                                 // change to that directory
  598.     }
  599.  
  600.  
  601.  
  602.  
  603.