File: appkill.c

package info (click to toggle)
penguin 19-3
  • links: PTS
  • area: contrib
  • in suites: etch, etch-m68k
  • size: 2,468 kB
  • ctags: 2,393
  • sloc: ansic: 13,993; makefile: 134
file content (109 lines) | stat: -rw-r--r-- 4,157 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*-
 * Copyright (c) 1995 Brian Gaeke.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE ABOVE COPYRIGHT HOLDERS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * I DIDN'T DO IT, NOBODY SAW ME DO IT, YOU CAN'T PROVE ANYTHING.
 */

/*
 * appkill.c 10/6/95 Brian Gaeke <brg@dgate.org>
 * 
 * Application quitting code, using Apple Events and Process Manager. I took
 * this straight out of an earlier program dated 28 December 1992. I've violated
 * my own copyright. Darn. I think I'll sue.
 *
 * I guess it just goes to show how out of touch I am with Mac programming that
 * I still consider Apple Events and the Process Manager "new". whoo. I guess I
 * quit learning when they quit sending me those free Developer CDs. :)
 *
 * One of the more significant improvements that could be made to this code
 * would be to run indent over it. The other would be to change all the variable
 * names to 'foo' with various random numbers appended. A somewhat less important
 * problem is that of error checking, because Allen said, "Don't stop the boot when
 * things go wrong." (Obviously Allen does not work for Saturn. heh... :)
 */
#include <string.h>
#include <AppleEvents.h>
#include <Processes.h>
#include <Errors.h>
#include "appkill_prototypes.h"

/* SendKill
 *
 * Sends QUIT apple event to the app with the specified process serial no.
 * No error checking, but it could be hacked in.
 *
 * If you are confused as to why this code doesn't work in your program,
 * try asking yourself whether the 'High Level Event Aware' bit is set in your
 * SIZE resource......
 */
void
SendKill(ProcessSerialNumber psn)
{
	AppleEvent		theEvent;
	AEAddressDesc	theAddress;

	memset(&theEvent,0,sizeof(theEvent));
	memset(&theAddress,0,sizeof(theAddress));
    AECreateDesc(typeProcessSerialNumber, (Ptr)&psn, sizeof(psn), &theAddress);
    AECreateAppleEvent(kCoreEventClass, kAEQuitApplication,
      &theAddress, kAutoGenerateReturnID, kAnyTransactionID, &theEvent);
    AESend(&theEvent, NULL, kAENoReply + kAEAlwaysInteract +
      kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
    AEDisposeDesc(&theAddress);
    AEDisposeDesc(&theEvent);
}

/* KillAllOtherApps
 *
 * Sends QUIT apple events to all other apps.
 * No error checking, but it could be hacked in without too much trouble.
 */
void
KillAllOtherApps(void)
{
	EventRecord				junkEvent;
	ProcessSerialNumber		psn, me;
	short					i;
	
	psn.highLongOfPSN = 0;
	psn.lowLongOfPSN = kNoProcess;
	me.highLongOfPSN = 0;
	me.lowLongOfPSN = kNoProcess;
	
	GetCurrentProcess(&me);
	while (GetNextProcess(&psn) != procNotFound)
	{
		/* Whaa, they said I couldn't != structs. It's only after being
		 * awake for 18+ hours that I begin to appreciate what C++ is for.
		 */
		if (memcmp(&psn,&me,sizeof(ProcessSerialNumber))!=0)
			SendKill(psn);
		/*
		 * Give the system a couple of events to shut down the app
		 */
		for(i = 0; i < 3; i++)
			WaitNextEvent(everyEvent, &junkEvent, 15, nil);
	}
}