home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / Puppeteer1.1 / Source / preferencesPuppet.m < prev    next >
Encoding:
Text File  |  1994-03-23  |  3.1 KB  |  126 lines

  1. /*
  2.  * Puppeteer 1.1
  3.  *
  4.  * Copyright (c) 1994 Primitive Software Ltd.  All rights reserved.
  5.  *
  6.  * Author: Dave Griffiths <dave@prim.demon.co.uk>
  7.  */
  8.  
  9. #import "Puppeteer.h"
  10. #import "WindowInfo.h"
  11.  
  12. /*
  13.  * preferencesPuppet. This program demonstrates the use of the Puppet object to
  14.  * toggle the Public Window Server switch from the command line. It assumes that
  15.  * you have no additional preferences modules loaded - if you have, you may need to
  16.  * adjust the x coordinate of one of the mouse clicks.
  17.  */
  18.  
  19. int
  20. waitForWindowCountToChange(id puppet, int currentWindowCount)
  21. /*
  22.  * Wait for the application's window count to change. Timeout after 5 seconds.
  23.  * Return the new window count.
  24.  */
  25. {
  26.     int i, newWindowCount;
  27.     
  28.     for (i=0; i<5; i++) {
  29.         if ((newWindowCount = [puppet windowCount]) != currentWindowCount)
  30.             break;
  31.         sleep(1);
  32.     }
  33.  
  34.     return newWindowCount;
  35. }
  36.  
  37. void
  38. togglePWS(id puppet)
  39. {
  40.     int oldWindowCount, newWindowCount;
  41.     id speaker = [puppet appSpeaker];
  42.  
  43.     /*
  44.      * Unhide Preview. Things don't work properly if the target application is
  45.      * hidden.
  46.      */
  47.     [speaker selectorRPC:"unhide" paramTypes:""];
  48.  
  49.     /*
  50.      * Make Preferences the active application.
  51.      */
  52.     [puppet postActivate:YES];
  53.     
  54.     /*
  55.      * Click in the far right of the slider for the top scroll view. The unix
  56.      * settings icon should then be second from the right.
  57.      */         
  58.     [puppet postSingleClick:NX_KEYWINDOW flags:0 x:377.0 y:212.0];
  59.     
  60.     /*
  61.      * Click on the unix settings icon. If you have other preference modules
  62.      * loaded, the x coordinate may need adjusting here.
  63.      */         
  64.     [puppet postSingleClick:NX_KEYWINDOW flags:0 x:278.0 y:256.0];
  65.     
  66.     /*
  67.      * Now click the "Public Window Server" switch. If this is currently off,
  68.      * an alert panel will pop up. To cope with this, we wait a few seconds to
  69.      * see if the window count changes. If it does, we can assume that there is
  70.      * an alert panel waiting and click it's OK button.
  71.      */         
  72.     oldWindowCount = [puppet windowCount];
  73.     [puppet postSingleClick:NX_KEYWINDOW flags:0 x:240.0 y:66.0];
  74.     newWindowCount = waitForWindowCountToChange(puppet, oldWindowCount);
  75.     if (newWindowCount != oldWindowCount) {
  76.         /*
  77.          * There must be an alert panel. Click OK. Tell the user what happened.
  78.          */
  79.         [puppet postSingleClick:NX_KEYWINDOW flags:0 x:328.0 y:20.0];
  80.         printf("Public Window Server enabled\n");
  81.     } else
  82.         printf("Public Window Server disabled\n");
  83. }
  84.  
  85. void
  86. main(argc, argv)
  87. int argc;
  88. char **argv;
  89. {
  90.     int c, errflg = 0;
  91.     id puppet;
  92.  
  93.     while ((c = getopt(argc, argv, "P")) != EOF)
  94.     switch (c) {
  95.     case '?':
  96.     default:
  97.         errflg++;
  98.         break;
  99.     }
  100.     if (errflg) {
  101.         fprintf(stderr, "Usage: preferencesPuppet [-P]\n");
  102.         exit(2);
  103.     }
  104.     
  105.     /*
  106.      * Create the Preferences puppet, launching Preferences if necessary.
  107.      */
  108.     puppet = [Puppeteer connectToApp:"Preferences" launch:YES];
  109.     if (!puppet) {
  110.         fprintf(stderr, "Could not connect to Preferences\n");
  111.         exit(1);
  112.     }
  113.  
  114.     /*
  115.      * Attach the strings. Preferences will then be ready to accept events.
  116.      */    
  117.     [puppet attachStrings];
  118.     
  119.     togglePWS(puppet);
  120.  
  121.     /*
  122.      * Release strings. This is necessary for Preferences to continue to respond to
  123.      * real user events.
  124.      */
  125.     [puppet releaseStrings];
  126. }