1 | /*
|
---|
2 | * Copyright (c) 2019 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup display
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file Display server output
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <assert.h>
|
---|
37 | #include <errno.h>
|
---|
38 | #include <fibril_synch.h>
|
---|
39 | #include <io/kbd_event.h>
|
---|
40 | #include <io/log.h>
|
---|
41 | #include <io/pos_event.h>
|
---|
42 | #include <loc.h>
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include "ddev.h"
|
---|
45 | #include "output.h"
|
---|
46 |
|
---|
47 | /** Check for new display devices.
|
---|
48 | *
|
---|
49 | * @param output Display server output
|
---|
50 | */
|
---|
51 | static errno_t ds_output_check_new_devs(ds_output_t *output)
|
---|
52 | {
|
---|
53 | category_id_t ddev_cid;
|
---|
54 | service_id_t *svcs;
|
---|
55 | size_t count, i;
|
---|
56 | bool already_known;
|
---|
57 | ds_ddev_t *nddev;
|
---|
58 | errno_t rc;
|
---|
59 |
|
---|
60 | assert(fibril_mutex_is_locked(&output->lock));
|
---|
61 |
|
---|
62 | rc = loc_category_get_id("display-device", &ddev_cid,
|
---|
63 | IPC_FLAG_BLOCKING);
|
---|
64 | if (rc != EOK) {
|
---|
65 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
66 | "Error looking up category 'display-device'.\n");
|
---|
67 | return EIO;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /*
|
---|
71 | * Check for new dispay devices
|
---|
72 | */
|
---|
73 | rc = loc_category_get_svcs(ddev_cid, &svcs, &count);
|
---|
74 | if (rc != EOK) {
|
---|
75 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
76 | "Error getting list of display devices.\n");
|
---|
77 | return EIO;
|
---|
78 | }
|
---|
79 |
|
---|
80 | for (i = 0; i < count; i++) {
|
---|
81 | already_known = false;
|
---|
82 |
|
---|
83 | /* Determine whether we already know this device. */
|
---|
84 | list_foreach(output->ddevs, loutdevs, ds_ddev_t, ddev) {
|
---|
85 | if (ddev->svc_id == svcs[i]) {
|
---|
86 | already_known = true;
|
---|
87 | break;
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | if (!already_known) {
|
---|
92 | rc = ds_ddev_open(output->def_display, svcs[i], &nddev);
|
---|
93 | if (rc != EOK) {
|
---|
94 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
95 | "Error adding display device.\n");
|
---|
96 | continue;
|
---|
97 | }
|
---|
98 |
|
---|
99 | list_append(&nddev->loutdevs, &output->ddevs);
|
---|
100 |
|
---|
101 | log_msg(LOG_DEFAULT, LVL_NOTE,
|
---|
102 | "Added display device '%lu'\n",
|
---|
103 | (unsigned long) svcs[i]);
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | free(svcs);
|
---|
108 |
|
---|
109 | return EOK;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /** Display device category change callback.
|
---|
113 | *
|
---|
114 | * @param arg Display server output (cast to void *)
|
---|
115 | */
|
---|
116 | static void ds_ddev_change_cb(void *arg)
|
---|
117 | {
|
---|
118 | ds_output_t *output = (ds_output_t *) arg;
|
---|
119 |
|
---|
120 | fibril_mutex_lock(&output->lock);
|
---|
121 | (void) ds_output_check_new_devs(output);
|
---|
122 | fibril_mutex_unlock(&output->lock);
|
---|
123 | }
|
---|
124 |
|
---|
125 | /** Create display server output.
|
---|
126 | *
|
---|
127 | * @param routput Place to store pointer to display server output object.
|
---|
128 | * @return EOK on success or an error code
|
---|
129 | */
|
---|
130 | errno_t ds_output_create(ds_output_t **routput)
|
---|
131 | {
|
---|
132 | ds_output_t *output;
|
---|
133 |
|
---|
134 | output = calloc(1, sizeof(ds_output_t));
|
---|
135 | if (output == NULL)
|
---|
136 | return ENOMEM;
|
---|
137 |
|
---|
138 | fibril_mutex_initialize(&output->lock);
|
---|
139 | list_initialize(&output->ddevs);
|
---|
140 |
|
---|
141 | *routput = output;
|
---|
142 | return EOK;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /** Start display device discovery.
|
---|
146 | *
|
---|
147 | * @param output Display server output
|
---|
148 | * @return EOK on success or an error code
|
---|
149 | */
|
---|
150 | errno_t ds_output_start_discovery(ds_output_t *output)
|
---|
151 | {
|
---|
152 | errno_t rc;
|
---|
153 |
|
---|
154 | rc = loc_register_cat_change_cb(ds_ddev_change_cb, output);
|
---|
155 | if (rc != EOK) {
|
---|
156 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
157 | "Failed registering callback for device discovery.\n");
|
---|
158 | return rc;
|
---|
159 | }
|
---|
160 |
|
---|
161 | fibril_mutex_lock(&output->lock);
|
---|
162 | rc = ds_output_check_new_devs(output);
|
---|
163 | fibril_mutex_unlock(&output->lock);
|
---|
164 |
|
---|
165 | /* Fail if we did not open at least one device */
|
---|
166 | if (list_empty(&output->ddevs)) {
|
---|
167 | log_msg(LOG_DEFAULT, LVL_ERROR, "No output device found.");
|
---|
168 | return ENOENT;
|
---|
169 | }
|
---|
170 |
|
---|
171 | return rc;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /** Destroy display server output.
|
---|
175 | *
|
---|
176 | * @param output Display server output
|
---|
177 | */
|
---|
178 | void ds_output_destroy(ds_output_t *output)
|
---|
179 | {
|
---|
180 | free(output);
|
---|
181 | }
|
---|
182 |
|
---|
183 | /** @}
|
---|
184 | */
|
---|