| 1 | /*
|
|---|
| 2 | * Copyright (c) 2014 Martin Decky
|
|---|
| 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 barber
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <unistd.h>
|
|---|
| 36 | #include <stdbool.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <stdio.h>
|
|---|
| 39 | #include <malloc.h>
|
|---|
| 40 | #include <task.h>
|
|---|
| 41 | #include <loc.h>
|
|---|
| 42 | #include <fibril_synch.h>
|
|---|
| 43 | #include <io/pixel.h>
|
|---|
| 44 | #include <device/led_dev.h>
|
|---|
| 45 | #include <window.h>
|
|---|
| 46 | #include <canvas.h>
|
|---|
| 47 | #include <surface.h>
|
|---|
| 48 | #include <codec/tga.gz.h>
|
|---|
| 49 | #include "images.h"
|
|---|
| 50 |
|
|---|
| 51 | #define NAME "barber"
|
|---|
| 52 |
|
|---|
| 53 | #define FRAMES 30
|
|---|
| 54 |
|
|---|
| 55 | #define FRAME_WIDTH 59
|
|---|
| 56 | #define FRAME_HEIGHT 192
|
|---|
| 57 |
|
|---|
| 58 | #define PERIOD 1000000
|
|---|
| 59 | #define COLORS 7
|
|---|
| 60 |
|
|---|
| 61 | static char *winreg = NULL;
|
|---|
| 62 | static fibril_timer_t *timer = NULL;
|
|---|
| 63 | static list_t led_devs;
|
|---|
| 64 |
|
|---|
| 65 | static canvas_t *frame_canvas;
|
|---|
| 66 | static surface_t *frames[FRAMES];
|
|---|
| 67 |
|
|---|
| 68 | static pixel_t colors[COLORS] = {
|
|---|
| 69 | PIXEL(0xff, 0xff, 0x00, 0x00),
|
|---|
| 70 | PIXEL(0xff, 0x00, 0xff, 0x00),
|
|---|
| 71 | PIXEL(0xff, 0x00, 0x00, 0xff),
|
|---|
| 72 | PIXEL(0xff, 0xff, 0xff, 0x00),
|
|---|
| 73 | PIXEL(0xff, 0xff, 0x00, 0xff),
|
|---|
| 74 | PIXEL(0xff, 0x00, 0xff, 0xff),
|
|---|
| 75 | PIXEL(0xff, 0xff, 0xff, 0xff)
|
|---|
| 76 | };
|
|---|
| 77 |
|
|---|
| 78 | static unsigned int frame = 0;
|
|---|
| 79 | static unsigned int color = 0;
|
|---|
| 80 |
|
|---|
| 81 | typedef struct {
|
|---|
| 82 | link_t link;
|
|---|
| 83 | service_id_t svc_id;
|
|---|
| 84 | async_sess_t *sess;
|
|---|
| 85 | } led_dev_t;
|
|---|
| 86 |
|
|---|
| 87 | static void timer_callback(void *data)
|
|---|
| 88 | {
|
|---|
| 89 | pixel_t next_color = colors[color];
|
|---|
| 90 |
|
|---|
| 91 | color++;
|
|---|
| 92 | if (color >= COLORS)
|
|---|
| 93 | color = 0;
|
|---|
| 94 |
|
|---|
| 95 | list_foreach(led_devs, link, led_dev_t, dev) {
|
|---|
| 96 | if (dev->sess)
|
|---|
| 97 | led_dev_color_set(dev->sess, next_color);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | frame++;
|
|---|
| 101 | if (frame >= FRAMES)
|
|---|
| 102 | frame = 0;
|
|---|
| 103 |
|
|---|
| 104 | update_canvas(frame_canvas, frames[frame]);
|
|---|
| 105 |
|
|---|
| 106 | fibril_timer_set(timer, PERIOD, timer_callback, NULL);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | static void loc_callback(void)
|
|---|
| 110 | {
|
|---|
| 111 | category_id_t led_cat;
|
|---|
| 112 | int rc = loc_category_get_id("led", &led_cat, IPC_FLAG_BLOCKING);
|
|---|
| 113 | if (rc != EOK)
|
|---|
| 114 | return;
|
|---|
| 115 |
|
|---|
| 116 | service_id_t *svcs;
|
|---|
| 117 | size_t count;
|
|---|
| 118 | rc = loc_category_get_svcs(led_cat, &svcs, &count);
|
|---|
| 119 | if (rc != EOK)
|
|---|
| 120 | return;
|
|---|
| 121 |
|
|---|
| 122 | for (size_t i = 0; i < count; i++) {
|
|---|
| 123 | bool known = false;
|
|---|
| 124 |
|
|---|
| 125 | /* Determine whether we already know this device. */
|
|---|
| 126 | list_foreach(led_devs, link, led_dev_t, dev) {
|
|---|
| 127 | if (dev->svc_id == svcs[i]) {
|
|---|
| 128 | known = true;
|
|---|
| 129 | break;
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | if (!known) {
|
|---|
| 134 | led_dev_t *dev = (led_dev_t *) calloc(1, sizeof(led_dev_t));
|
|---|
| 135 | if (!dev)
|
|---|
| 136 | continue;
|
|---|
| 137 |
|
|---|
| 138 | link_initialize(&dev->link);
|
|---|
| 139 | dev->svc_id = svcs[i];
|
|---|
| 140 | dev->sess = loc_service_connect(EXCHANGE_SERIALIZE, svcs[i], 0);
|
|---|
| 141 |
|
|---|
| 142 | list_append(&dev->link, &led_devs);
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | // FIXME: Handle LED device removal
|
|---|
| 147 |
|
|---|
| 148 | free(svcs);
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | static bool decode_frames(void)
|
|---|
| 152 | {
|
|---|
| 153 | frames[0] = decode_tga_gz((void *) frame01_tga_gz, frame01_tga_gz_size, 0);
|
|---|
| 154 | frames[1] = decode_tga_gz((void *) frame02_tga_gz, frame02_tga_gz_size, 0);
|
|---|
| 155 | frames[2] = decode_tga_gz((void *) frame03_tga_gz, frame03_tga_gz_size, 0);
|
|---|
| 156 | frames[3] = decode_tga_gz((void *) frame04_tga_gz, frame04_tga_gz_size, 0);
|
|---|
| 157 | frames[4] = decode_tga_gz((void *) frame05_tga_gz, frame05_tga_gz_size, 0);
|
|---|
| 158 | frames[5] = decode_tga_gz((void *) frame06_tga_gz, frame06_tga_gz_size, 0);
|
|---|
| 159 | frames[6] = decode_tga_gz((void *) frame07_tga_gz, frame07_tga_gz_size, 0);
|
|---|
| 160 | frames[7] = decode_tga_gz((void *) frame08_tga_gz, frame08_tga_gz_size, 0);
|
|---|
| 161 | frames[8] = decode_tga_gz((void *) frame09_tga_gz, frame09_tga_gz_size, 0);
|
|---|
| 162 | frames[9] = decode_tga_gz((void *) frame10_tga_gz, frame10_tga_gz_size, 0);
|
|---|
| 163 | frames[10] = decode_tga_gz((void *) frame11_tga_gz, frame11_tga_gz_size, 0);
|
|---|
| 164 | frames[11] = decode_tga_gz((void *) frame12_tga_gz, frame12_tga_gz_size, 0);
|
|---|
| 165 | frames[12] = decode_tga_gz((void *) frame13_tga_gz, frame13_tga_gz_size, 0);
|
|---|
| 166 | frames[13] = decode_tga_gz((void *) frame14_tga_gz, frame14_tga_gz_size, 0);
|
|---|
| 167 | frames[14] = decode_tga_gz((void *) frame15_tga_gz, frame15_tga_gz_size, 0);
|
|---|
| 168 | frames[15] = decode_tga_gz((void *) frame16_tga_gz, frame16_tga_gz_size, 0);
|
|---|
| 169 | frames[16] = decode_tga_gz((void *) frame17_tga_gz, frame17_tga_gz_size, 0);
|
|---|
| 170 | frames[17] = decode_tga_gz((void *) frame18_tga_gz, frame18_tga_gz_size, 0);
|
|---|
| 171 | frames[18] = decode_tga_gz((void *) frame19_tga_gz, frame19_tga_gz_size, 0);
|
|---|
| 172 | frames[19] = decode_tga_gz((void *) frame20_tga_gz, frame20_tga_gz_size, 0);
|
|---|
| 173 | frames[20] = decode_tga_gz((void *) frame21_tga_gz, frame21_tga_gz_size, 0);
|
|---|
| 174 | frames[21] = decode_tga_gz((void *) frame22_tga_gz, frame22_tga_gz_size, 0);
|
|---|
| 175 | frames[22] = decode_tga_gz((void *) frame23_tga_gz, frame23_tga_gz_size, 0);
|
|---|
| 176 | frames[23] = decode_tga_gz((void *) frame24_tga_gz, frame24_tga_gz_size, 0);
|
|---|
| 177 | frames[24] = decode_tga_gz((void *) frame25_tga_gz, frame25_tga_gz_size, 0);
|
|---|
| 178 | frames[25] = decode_tga_gz((void *) frame26_tga_gz, frame26_tga_gz_size, 0);
|
|---|
| 179 | frames[26] = decode_tga_gz((void *) frame27_tga_gz, frame27_tga_gz_size, 0);
|
|---|
| 180 | frames[27] = decode_tga_gz((void *) frame28_tga_gz, frame28_tga_gz_size, 0);
|
|---|
| 181 | frames[28] = decode_tga_gz((void *) frame29_tga_gz, frame29_tga_gz_size, 0);
|
|---|
| 182 | frames[29] = decode_tga_gz((void *) frame30_tga_gz, frame30_tga_gz_size, 0);
|
|---|
| 183 |
|
|---|
| 184 | for (unsigned int frame = 0; frame < FRAMES; frame++) {
|
|---|
| 185 | if (frames[frame] == NULL) {
|
|---|
| 186 | printf("Unable to decode frame %u.\n", frame);
|
|---|
| 187 | return false;
|
|---|
| 188 | }
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | return true;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | int main(int argc, char *argv[])
|
|---|
| 195 | {
|
|---|
| 196 | if (argc < 2) {
|
|---|
| 197 | printf("Compositor server not specified.\n");
|
|---|
| 198 | return 1;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | list_initialize(&led_devs);
|
|---|
| 202 | int rc = loc_register_cat_change_cb(loc_callback);
|
|---|
| 203 | if (rc != EOK) {
|
|---|
| 204 | printf("Unable to register callback for device discovery.\n");
|
|---|
| 205 | return 1;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | timer = fibril_timer_create(NULL);
|
|---|
| 209 | if (!timer) {
|
|---|
| 210 | printf("Unable to create timer.\n");
|
|---|
| 211 | return 1;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | if (!decode_frames())
|
|---|
| 215 | return 1;
|
|---|
| 216 |
|
|---|
| 217 | winreg = argv[1];
|
|---|
| 218 | window_t *main_window = window_open(argv[1], true, true, "barber");
|
|---|
| 219 | if (!main_window) {
|
|---|
| 220 | printf("Cannot open main window.\n");
|
|---|
| 221 | return 1;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | frame_canvas = create_canvas(window_root(main_window),
|
|---|
| 225 | FRAME_WIDTH, FRAME_HEIGHT, frames[frame]);
|
|---|
| 226 |
|
|---|
| 227 | if (!frame_canvas) {
|
|---|
| 228 | window_close(main_window);
|
|---|
| 229 | printf("Cannot create widgets.\n");
|
|---|
| 230 | return 1;
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | window_resize(main_window, 0, 0, FRAME_WIDTH + 8, FRAME_HEIGHT + 28,
|
|---|
| 234 | WINDOW_PLACEMENT_RIGHT | WINDOW_PLACEMENT_BOTTOM);
|
|---|
| 235 | window_exec(main_window);
|
|---|
| 236 |
|
|---|
| 237 | fibril_timer_set(timer, PERIOD, timer_callback, NULL);
|
|---|
| 238 |
|
|---|
| 239 | task_retval(0);
|
|---|
| 240 | async_manager();
|
|---|
| 241 |
|
|---|
| 242 | return 0;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | /** @}
|
|---|
| 246 | */
|
|---|