summaryrefslogtreecommitdiff
path: root/hw/xfree86/os-support/linux/lnx_acpi.c
blob: bb975cc5d3569675a9d295e3b0fd8f7738952aa6 (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#ifdef HAVE_XORG_CONFIG_H
#include "xorg-config.h"
#endif

#include "os.h"
#include "xf86.h"
#include "xf86Priv.h"
#define XF86_OS_PRIVS
#include "xf86_OSproc.h"
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
 
#define ACPI_SOCKET  "/var/run/acpid.socket"

#define ACPI_VIDEO_NOTIFY_SWITCH	0x80
#define ACPI_VIDEO_NOTIFY_PROBE		0x81
#define ACPI_VIDEO_NOTIFY_CYCLE		0x82
#define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT	0x83
#define ACPI_VIDEO_NOTIFY_PREV_OUTPUT	0x84

#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS	0x85
#define	ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS	0x86
#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS	0x87
#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS	0x88
#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF		0x89

#define ACPI_VIDEO_HEAD_INVALID		(~0u - 1)
#define ACPI_VIDEO_HEAD_END		(~0u)

static void lnxCloseACPI(void);
static pointer ACPIihPtr = NULL;
PMClose lnxACPIOpen(void);

/* in milliseconds */
#define ACPI_REOPEN_DELAY 1000

static CARD32
lnxACPIReopen(OsTimerPtr timer, CARD32 time, pointer arg)
{
    if (lnxACPIOpen()) {
	TimerFree(timer);
	return 0;
    }

    return ACPI_REOPEN_DELAY;
}

#define LINE_LENGTH 80

static int
lnxACPIGetEventFromOs(int fd, pmEvent *events, int num)
{
    char ev[LINE_LENGTH];
    int n;

    memset(ev, 0, LINE_LENGTH);

    do {
	n = read( fd, ev, LINE_LENGTH );
    } while ((n == -1) && (errno == EAGAIN || errno == EINTR));

    if (n <= 0) {
	lnxCloseACPI();
	TimerSet(NULL, 0, ACPI_REOPEN_DELAY, lnxACPIReopen, NULL);
	return 0;
    }
    /* FIXME: this only processes the first read ACPI event & might break
     * with interrupted reads. */
    
    /* Check that we have a video event */
    if (!strncmp(ev, "video", 5)) {
	char *video = NULL;
	char *GFX = NULL;
	char *notify = NULL;
	char *data = NULL; /* doesn't appear to be used in the kernel */
	unsigned long int notify_l, data_l;

	video = strtok(ev, " ");

	GFX = strtok(NULL, " ");
#if 0
	ErrorF("GFX: %s\n",GFX);
#endif

	notify = strtok(NULL, " ");
	notify_l = strtoul(notify, NULL, 16);
#if 0
	ErrorF("notify: 0x%lx\n",notify_l);
#endif

	data = strtok(NULL, " ");
	data_l = strtoul(data, NULL, 16);
#if 0
	ErrorF("data: 0x%lx\n",data_l);
#endif

	/* Differentiate between events */
	switch (notify_l) {
		case ACPI_VIDEO_NOTIFY_SWITCH:
		case ACPI_VIDEO_NOTIFY_CYCLE:
		case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT:
		case ACPI_VIDEO_NOTIFY_PREV_OUTPUT:
		    events[0] = XF86_APM_CAPABILITY_CHANGED;
		    return 1;
		case ACPI_VIDEO_NOTIFY_PROBE:
		    return 0;
		default:
		    return 0;
	}
    }
    
    return 0;
}

static pmWait
lnxACPIConfirmEventToOs(int fd, pmEvent event)
{
    /* No ability to send back to the kernel in ACPI */
    switch (event) {
    default:
	return PM_NONE;
    }
}

PMClose
lnxACPIOpen(void)
{
    int fd;    
    struct sockaddr_un addr;
    int r = -1;

#ifdef DEBUG
    ErrorF("ACPI: OSPMOpen called\n");
#endif
    if (ACPIihPtr || !xf86Info.pmFlag)
	return NULL;
   
#ifdef DEBUG
    ErrorF("ACPI: Opening device\n");
#endif
    if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) > -1) {
	memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;
	strcpy(addr.sun_path, ACPI_SOCKET);
	if ((r = connect(fd, (struct sockaddr*)&addr, sizeof(addr))) == -1) {
	    xf86MsgVerb(X_WARNING,3,"Open ACPI failed (%s) (%s)\n", ACPI_SOCKET,
	    	strerror(errno));
	    shutdown(fd, 2);
	    close(fd);
	    return NULL;
    	}
    }

    xf86PMGetEventFromOs = lnxACPIGetEventFromOs;
    xf86PMConfirmEventToOs = lnxACPIConfirmEventToOs;
    ACPIihPtr = xf86AddGeneralHandler(fd,xf86HandlePMEvents,NULL);
    xf86MsgVerb(X_INFO,3,"Open ACPI successful (%s)\n", ACPI_SOCKET);

    return lnxCloseACPI;
}

static void
lnxCloseACPI(void)
{
    int fd;
    
#ifdef DEBUG
   ErrorF("ACPI: Closing device\n");
#endif
    if (ACPIihPtr) {
	fd = xf86RemoveGeneralHandler(ACPIihPtr);
	shutdown(fd, 2);
	close(fd);
	ACPIihPtr = NULL;
    }
}