| 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/pos_event.h>
|
|---|
| 41 | #include <loc.h>
|
|---|
| 42 | #include <stdio.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 | printf("Error looking up category 'display-device'.\n");
|
|---|
| 66 | return EIO;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /*
|
|---|
| 70 | * Check for new dispay devices
|
|---|
| 71 | */
|
|---|
| 72 | rc = loc_category_get_svcs(ddev_cid, &svcs, &count);
|
|---|
| 73 | if (rc != EOK) {
|
|---|
| 74 | printf("Error getting list of display devices.\n");
|
|---|
| 75 | return EIO;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | for (i = 0; i < count; i++) {
|
|---|
| 79 | already_known = false;
|
|---|
| 80 |
|
|---|
| 81 | /* Determine whether we already know this device. */
|
|---|
| 82 | list_foreach(output->ddevs, loutdevs, ds_ddev_t, ddev) {
|
|---|
| 83 | if (ddev->svc_id == svcs[i]) {
|
|---|
| 84 | already_known = true;
|
|---|
| 85 | break;
|
|---|
| 86 | }
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | if (!already_known) {
|
|---|
| 90 | rc = ds_ddev_open(output->def_display, svcs[i], &nddev);
|
|---|
| 91 | if (rc != EOK) {
|
|---|
| 92 | printf("Error adding display device.\n");
|
|---|
| 93 | continue;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | list_append(&nddev->loutdevs, &output->ddevs);
|
|---|
| 97 |
|
|---|
| 98 | printf("Added display device '%lu'\n",
|
|---|
| 99 | (unsigned long) svcs[i]);
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | free(svcs);
|
|---|
| 104 |
|
|---|
| 105 | return EOK;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /** Display device category change callback.
|
|---|
| 109 | *
|
|---|
| 110 | * @param arg Display server output (cast to void *)
|
|---|
| 111 | */
|
|---|
| 112 | static void ds_ddev_change_cb(void *arg)
|
|---|
| 113 | {
|
|---|
| 114 | ds_output_t *output = (ds_output_t *) arg;
|
|---|
| 115 |
|
|---|
| 116 | printf("ds_ddev_change_cb\n");
|
|---|
| 117 | fibril_mutex_lock(&output->lock);
|
|---|
| 118 | (void) ds_output_check_new_devs(output);
|
|---|
| 119 | fibril_mutex_unlock(&output->lock);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /** Create display server output.
|
|---|
| 123 | *
|
|---|
| 124 | * @param routput Place to store pointer to display server output object.
|
|---|
| 125 | * @return EOK on success or an error code
|
|---|
| 126 | */
|
|---|
| 127 | errno_t ds_output_create(ds_output_t **routput)
|
|---|
| 128 | {
|
|---|
| 129 | ds_output_t *output;
|
|---|
| 130 |
|
|---|
| 131 | output = calloc(1, sizeof(ds_output_t));
|
|---|
| 132 | if (output == NULL)
|
|---|
| 133 | return ENOMEM;
|
|---|
| 134 |
|
|---|
| 135 | fibril_mutex_initialize(&output->lock);
|
|---|
| 136 | list_initialize(&output->ddevs);
|
|---|
| 137 |
|
|---|
| 138 | *routput = output;
|
|---|
| 139 | return EOK;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /** Start display device discovery.
|
|---|
| 143 | *
|
|---|
| 144 | * @param output Display server output
|
|---|
| 145 | * @return EOK on success or an error code
|
|---|
| 146 | */
|
|---|
| 147 | errno_t ds_output_start_discovery(ds_output_t *output)
|
|---|
| 148 | {
|
|---|
| 149 | errno_t rc;
|
|---|
| 150 |
|
|---|
| 151 | rc = loc_register_cat_change_cb(ds_ddev_change_cb, output);
|
|---|
| 152 | if (rc != EOK) {
|
|---|
| 153 | printf("Failed registering callback for device discovery.\n");
|
|---|
| 154 | return rc;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | fibril_mutex_lock(&output->lock);
|
|---|
| 158 | rc = ds_output_check_new_devs(output);
|
|---|
| 159 | fibril_mutex_unlock(&output->lock);
|
|---|
| 160 |
|
|---|
| 161 | return rc;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | /** Destroy display server output.
|
|---|
| 165 | *
|
|---|
| 166 | * @param output Display server output
|
|---|
| 167 | */
|
|---|
| 168 | void ds_output_destroy(ds_output_t *output)
|
|---|
| 169 | {
|
|---|
| 170 | free(output);
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | /** @}
|
|---|
| 174 | */
|
|---|