xref: /freebsd/usr.sbin/apmd/apmd.h (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1d50a71bdSMitsuru IWASAKI /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
4d50a71bdSMitsuru IWASAKI  * APM (Advanced Power Management) Event Dispatcher
5d50a71bdSMitsuru IWASAKI  *
6d50a71bdSMitsuru IWASAKI  * Copyright (c) 1999 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
7d50a71bdSMitsuru IWASAKI  * Copyright (c) 1999 KOIE Hidetaka <koie@suri.co.jp>
8d50a71bdSMitsuru IWASAKI  * All rights reserved.
9d50a71bdSMitsuru IWASAKI  *
10d50a71bdSMitsuru IWASAKI  * Redistribution and use in source and binary forms, with or without
11d50a71bdSMitsuru IWASAKI  * modification, are permitted provided that the following conditions
12d50a71bdSMitsuru IWASAKI  * are met:
13d50a71bdSMitsuru IWASAKI  * 1. Redistributions of source code must retain the above copyright
14d50a71bdSMitsuru IWASAKI  *    notice, this list of conditions and the following disclaimer.
15d50a71bdSMitsuru IWASAKI  * 2. Redistributions in binary form must reproduce the above copyright
16d50a71bdSMitsuru IWASAKI  *    notice, this list of conditions and the following disclaimer in the
17d50a71bdSMitsuru IWASAKI  *    documentation and/or other materials provided with the distribution.
18d50a71bdSMitsuru IWASAKI  *
19d50a71bdSMitsuru IWASAKI  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20d50a71bdSMitsuru IWASAKI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21d50a71bdSMitsuru IWASAKI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22d50a71bdSMitsuru IWASAKI  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23d50a71bdSMitsuru IWASAKI  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24d50a71bdSMitsuru IWASAKI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25d50a71bdSMitsuru IWASAKI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26d50a71bdSMitsuru IWASAKI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27d50a71bdSMitsuru IWASAKI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28d50a71bdSMitsuru IWASAKI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29d50a71bdSMitsuru IWASAKI  * SUCH DAMAGE.
30d50a71bdSMitsuru IWASAKI  */
31d50a71bdSMitsuru IWASAKI 
32d50a71bdSMitsuru IWASAKI #define APMD_CONFIGFILE		"/etc/apmd.conf"
33d50a71bdSMitsuru IWASAKI #define APM_CTL_DEVICEFILE	"/dev/apmctl"
34719b9dc1SNick Sayer #define APM_NORM_DEVICEFILE	"/dev/apm"
35d50a71bdSMitsuru IWASAKI #define APMD_PIDFILE		"/var/run/apmd.pid"
36d50a71bdSMitsuru IWASAKI #define NICE_INCR		-20
37d50a71bdSMitsuru IWASAKI 
38d50a71bdSMitsuru IWASAKI enum {
39d50a71bdSMitsuru IWASAKI 	EVENT_NOEVENT,
40d50a71bdSMitsuru IWASAKI 	EVENT_STANDBYREQ,
41d50a71bdSMitsuru IWASAKI 	EVENT_SUSPENDREQ,
42d50a71bdSMitsuru IWASAKI 	EVENT_NORMRESUME,
43d50a71bdSMitsuru IWASAKI 	EVENT_CRITRESUME,
44d50a71bdSMitsuru IWASAKI 	EVENT_BATTERYLOW,
45d50a71bdSMitsuru IWASAKI 	EVENT_POWERSTATECHANGE,
46d50a71bdSMitsuru IWASAKI 	EVENT_UPDATETIME,
47d50a71bdSMitsuru IWASAKI 	EVENT_CRITSUSPEND,
48d50a71bdSMitsuru IWASAKI 	EVENT_USERSTANDBYREQ,
49d50a71bdSMitsuru IWASAKI 	EVENT_USERSUSPENDREQ,
50d50a71bdSMitsuru IWASAKI 	EVENT_STANDBYRESUME,
51d50a71bdSMitsuru IWASAKI 	EVENT_CAPABILITIESCHANGE,
52d50a71bdSMitsuru IWASAKI 	EVENT_MAX
53d50a71bdSMitsuru IWASAKI };
54d50a71bdSMitsuru IWASAKI 
55d50a71bdSMitsuru IWASAKI struct event_cmd_op {
56d89167b4SAlfred Perlstein 	int (* act)(void *this);
57d89167b4SAlfred Perlstein 	void (* dump)(void *this, FILE * fp);
58d89167b4SAlfred Perlstein 	struct event_cmd * (* clone)(void *this);
59d89167b4SAlfred Perlstein 	void (* free)(void *this);
60d50a71bdSMitsuru IWASAKI };
61d50a71bdSMitsuru IWASAKI struct event_cmd {
62d50a71bdSMitsuru IWASAKI 	struct event_cmd * next;
63d50a71bdSMitsuru IWASAKI 	size_t len;
64d50a71bdSMitsuru IWASAKI 	char * name;
65d50a71bdSMitsuru IWASAKI 	struct event_cmd_op * op;
66d50a71bdSMitsuru IWASAKI };
67d50a71bdSMitsuru IWASAKI struct event_cmd_exec {
68d50a71bdSMitsuru IWASAKI 	struct event_cmd evcmd;
69d50a71bdSMitsuru IWASAKI 	char * line;		/* Command line */
70d50a71bdSMitsuru IWASAKI };
71d50a71bdSMitsuru IWASAKI struct event_cmd_reject {
72d50a71bdSMitsuru IWASAKI 	struct event_cmd evcmd;
73d50a71bdSMitsuru IWASAKI };
74d50a71bdSMitsuru IWASAKI 
75d50a71bdSMitsuru IWASAKI struct event_config {
76d50a71bdSMitsuru IWASAKI 	const char *name;
77d50a71bdSMitsuru IWASAKI 	struct event_cmd * cmdlist;
78d50a71bdSMitsuru IWASAKI 	int rejectable;
79d50a71bdSMitsuru IWASAKI };
80d50a71bdSMitsuru IWASAKI 
81719b9dc1SNick Sayer struct battery_watch_event {
82719b9dc1SNick Sayer 	struct battery_watch_event *next;
83719b9dc1SNick Sayer 	int level;
84719b9dc1SNick Sayer 	enum {
85719b9dc1SNick Sayer 		BATTERY_CHARGING,
86719b9dc1SNick Sayer 		BATTERY_DISCHARGING
87719b9dc1SNick Sayer 	} direction;
88719b9dc1SNick Sayer 	enum {
89719b9dc1SNick Sayer 		BATTERY_MINUTES,
90719b9dc1SNick Sayer 		BATTERY_PERCENT
91719b9dc1SNick Sayer 	} type;
92719b9dc1SNick Sayer 	int done;
93719b9dc1SNick Sayer 	struct event_cmd *cmdlist;
94719b9dc1SNick Sayer };
95719b9dc1SNick Sayer 
96719b9dc1SNick Sayer 
97d50a71bdSMitsuru IWASAKI extern struct event_cmd_op event_cmd_exec_ops;
98d50a71bdSMitsuru IWASAKI extern struct event_cmd_op event_cmd_reject_ops;
99d50a71bdSMitsuru IWASAKI extern struct event_config events[EVENT_MAX];
100719b9dc1SNick Sayer extern struct battery_watch_event *battery_watch_list;
101d50a71bdSMitsuru IWASAKI 
102719b9dc1SNick Sayer extern int register_battery_handlers(
103719b9dc1SNick Sayer 	int level, int direction,
104719b9dc1SNick Sayer 	struct event_cmd *cmdlist);
105d50a71bdSMitsuru IWASAKI extern int register_apm_event_handlers(
106d50a71bdSMitsuru IWASAKI 	bitstr_t bit_decl(evlist, EVENT_MAX),
107d50a71bdSMitsuru IWASAKI 	struct event_cmd *cmdlist);
108d50a71bdSMitsuru IWASAKI extern void free_event_cmd_list(struct event_cmd *p);
109103e4932SUlrich Spörlein 
110103e4932SUlrich Spörlein extern int	yyparse(void);
111103e4932SUlrich Spörlein 
112103e4932SUlrich Spörlein void	yyerror(const char *);
113103e4932SUlrich Spörlein int	yylex(void);
114103e4932SUlrich Spörlein 
115103e4932SUlrich Spörlein struct event_cmd *event_cmd_default_clone(void *);
116103e4932SUlrich Spörlein int event_cmd_exec_act(void *);
117103e4932SUlrich Spörlein void event_cmd_exec_dump(void *, FILE *);
118103e4932SUlrich Spörlein struct event_cmd *event_cmd_exec_clone(void *);
119103e4932SUlrich Spörlein void event_cmd_exec_free(void *);
120103e4932SUlrich Spörlein int event_cmd_reject_act(void *);
121103e4932SUlrich Spörlein struct event_cmd *clone_event_cmd_list(struct event_cmd *);
122103e4932SUlrich Spörlein int exec_run_cmd(struct event_cmd *);
123103e4932SUlrich Spörlein int exec_event_cmd(struct event_config *);
124103e4932SUlrich Spörlein void read_config(void);
125103e4932SUlrich Spörlein void dump_config(void);
126103e4932SUlrich Spörlein void destroy_config(void);
127103e4932SUlrich Spörlein void restart(void);
128103e4932SUlrich Spörlein void enque_signal(int);
129103e4932SUlrich Spörlein void wait_child(void);
130103e4932SUlrich Spörlein int proc_signal(int);
131103e4932SUlrich Spörlein void proc_apmevent(int);
132103e4932SUlrich Spörlein void check_battery(void);
133103e4932SUlrich Spörlein void event_loop(void);
134