summaryrefslogtreecommitdiff
path: root/src/touchscreen/touchscreen.c
blob: 45caf747cffd6a7d351d3fe6e240afe695d5d049 (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
/*
 * deviced
 *
 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <assert.h>
#include <stdbool.h>
#include <hw/touchscreen.h>
#include "core/devices.h"
#include "core/common.h"
#include "core/log.h"

static struct touchscreen_device *touchscreen_dev;

static int touchscreen_probe(void *data)
{
	int ret;
	struct hw_info *info;

	if (touchscreen_dev)
		return 0;

	ret = hw_get_info(TOUCHSCREEN_HARDWARE_DEVICE_ID,
			(const struct hw_info **)&info);
	if (ret < 0) {
		_E("Failed to load touchscreen shared library: %d", ret);
		return -ENODEV;
	}

	if (!info->open) {
		_E("Failed to open touchscreen device: open(NULL)");
		return -EPERM;
	}

	ret = info->open(info, NULL, (struct hw_common **)&touchscreen_dev);
	if (ret < 0) {
		_E("Failed to get touchscreen device structure: %d", ret);
		return -EPERM;
	}

	_I("Touchscreen device structure load success.");
	return 0;
}

static void touchscreen_exit(void *data)
{
	struct hw_info *info;

	if (!touchscreen_dev)
		return;

	info = touchscreen_dev->common.info;
	assert(info);

	info->close((struct hw_common *)touchscreen_dev);
	touchscreen_dev = NULL;
}

static int touchscreen_set_state(enum touchscreen_state state)
{
	int ret;
	char *act;

	if (!touchscreen_dev) {
		_E("Touchscreen device structure is not loaded.");
		return -ENOENT;
	}

	if (state != TOUCHSCREEN_ON && state != TOUCHSCREEN_OFF) {
		_E("Invalid parameter.");
		return -EINVAL;
	}

	if (!touchscreen_dev->set_state) {
		_E("Touchscreen state change is not supported.");
		return -ENOTSUP;
	}

	act = (state == TOUCHSCREEN_ON) ? "enable" : "disable";

	ret = touchscreen_dev->set_state(state);
	if (ret == 0)
		_I("Success to %s touchscreen.", act);
	else
		_E("Failed to %s touchscreen: %d", act, ret);

	return ret;
}

static int touchscreen_start(enum device_flags flags)
{
	return touchscreen_set_state(TOUCHSCREEN_ON);
}

static int touchscreen_stop(enum device_flags flags)
{
	if (flags & AMBIENT_MODE)
		return 0;
	return touchscreen_set_state(TOUCHSCREEN_OFF);
}

static int touchscreen_dump(FILE *fp, int mode, void *dump_data)
{
	int ret;
	enum touchscreen_state state;

	if (!touchscreen_dev)
		return 0;

	ret = touchscreen_dev->get_state(&state);
	if (ret < 0)
		_E("Failed to get touchscreen state: %d", ret);

	if (state == TOUCHSCREEN_ON)
		LOG_DUMP(fp, "Touchscreen is enabled\n");
	else
		LOG_DUMP(fp, "Touchscreen is disabled\n");

	return 0;
}

static const struct device_ops touchscreen_device_ops = {
	DECLARE_NAME_LEN("touchscreen"),
	.probe    = touchscreen_probe,
	.exit     = touchscreen_exit,
	.start    = touchscreen_start,
	.stop     = touchscreen_stop,
	.dump     = touchscreen_dump,
};

DEVICE_OPS_REGISTER(&touchscreen_device_ops)