source: mainline/uspace/app/vlaunch/vlaunch.c@ 6bcecc2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6bcecc2 was b5674b2, checked in by Martin Decky <martin@…>, 11 years ago

cycle periodically the color of connected LED devices in vlaunch
(this can be used as a poor man's debugging tool to detect if the user space is still alive)

  • Property mode set to 100644
File size: 6.7 KB
Line 
1/*
2 * Copyright (c) 2012 Petr Koupy
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 vlaunch
30 * @{
31 */
32/** @file
33 */
34
35#include <stdbool.h>
36#include <errno.h>
37#include <stdio.h>
38#include <malloc.h>
39#include <io/pixel.h>
40#include <task.h>
41#include <str.h>
42#include <str_error.h>
43#include <loc.h>
44#include <fibril_synch.h>
45#include <io/pixel.h>
46#include <device/led_dev.h>
47
48#include <window.h>
49#include <grid.h>
50#include <button.h>
51#include <label.h>
52#include <canvas.h>
53
54#include <surface.h>
55#include <source.h>
56#include <drawctx.h>
57#include <codec/tga.h>
58
59#include "images.h"
60
61#define NAME "vlaunch"
62
63#define LOGO_WIDTH 196
64#define LOGO_HEIGHT 66
65
66#define PERIOD 1000000
67#define COLORS 7
68
69static char *winreg = NULL;
70static fibril_timer_t *timer = NULL;
71static list_t led_devs;
72
73static pixel_t colors[COLORS] = {
74 PIXEL(0xff, 0xff, 0x00, 0x00),
75 PIXEL(0xff, 0x00, 0xff, 0x00),
76 PIXEL(0xff, 0x00, 0x00, 0xff),
77 PIXEL(0xff, 0xff, 0xff, 0x00),
78 PIXEL(0xff, 0xff, 0x00, 0xff),
79 PIXEL(0xff, 0x00, 0xff, 0xff),
80 PIXEL(0xff, 0xff, 0xff, 0xff)
81};
82
83static unsigned int color = 0;
84
85typedef struct {
86 link_t link;
87 service_id_t svc_id;
88 async_sess_t *sess;
89} led_dev_t;
90
91static int app_launch(const char *app)
92{
93 printf("%s: Spawning %s %s \n", NAME, app, winreg);
94
95 task_id_t id;
96 int rc = task_spawnl(&id, app, app, winreg, NULL);
97 if (rc != EOK) {
98 printf("%s: Error spawning %s %s (%s)\n", NAME, app,
99 winreg, str_error(rc));
100 return -1;
101 }
102
103 task_exit_t texit;
104 int retval;
105 rc = task_wait(id, &texit, &retval);
106 if ((rc != EOK) || (texit != TASK_EXIT_NORMAL)) {
107 printf("%s: Error retrieving retval from %s (%s)\n", NAME,
108 app, str_error(rc));
109 return -1;
110 }
111
112 return retval;
113}
114
115static void on_vterm(widget_t *widget, void *data)
116{
117 app_launch("/app/vterm");
118}
119
120static void on_vdemo(widget_t *widget, void *data)
121{
122 app_launch("/app/vdemo");
123}
124
125static void on_vlaunch(widget_t *widget, void *data)
126{
127 app_launch("/app/vlaunch");
128}
129
130static void timer_callback(void *data)
131{
132 pixel_t next_color = colors[color];
133
134 color++;
135 if (color >= COLORS)
136 color = 0;
137
138 list_foreach(led_devs, link, led_dev_t, dev) {
139 if (dev->sess)
140 led_dev_color_set(dev->sess, next_color);
141 }
142
143 fibril_timer_set(timer, PERIOD, timer_callback, NULL);
144}
145
146static void loc_callback(void)
147{
148 category_id_t led_cat;
149 int rc = loc_category_get_id("led", &led_cat, IPC_FLAG_BLOCKING);
150 if (rc != EOK)
151 return;
152
153 service_id_t *svcs;
154 size_t count;
155 rc = loc_category_get_svcs(led_cat, &svcs, &count);
156 if (rc != EOK)
157 return;
158
159 for (size_t i = 0; i < count; i++) {
160 bool known = false;
161
162 /* Determine whether we already know this device. */
163 list_foreach(led_devs, link, led_dev_t, dev) {
164 if (dev->svc_id == svcs[i]) {
165 known = true;
166 break;
167 }
168 }
169
170 if (!known) {
171 led_dev_t *dev = (led_dev_t *) calloc(1, sizeof(led_dev_t));
172 if (!dev)
173 continue;
174
175 link_initialize(&dev->link);
176 dev->svc_id = svcs[i];
177 dev->sess = loc_service_connect(EXCHANGE_SERIALIZE, svcs[i], 0);
178
179 list_append(&dev->link, &led_devs);
180 }
181 }
182
183 // FIXME: Handle LED device removal
184
185 free(svcs);
186}
187
188int main(int argc, char *argv[])
189{
190 if (argc < 2) {
191 printf("Compositor server not specified.\n");
192 return 1;
193 }
194
195 list_initialize(&led_devs);
196 int rc = loc_register_cat_change_cb(loc_callback);
197 if (rc != EOK) {
198 printf("Unable to register callback for device discovery.\n");
199 return 1;
200 }
201
202 timer = fibril_timer_create();
203 if (!timer) {
204 printf("Unable to create timer.\n");
205 return 1;
206 }
207
208 surface_t *logo = decode_tga((void *) helenos_tga, helenos_tga_size, 0);
209 if (!logo) {
210 printf("Unable to decode logo.\n");
211 return 1;
212 }
213
214 winreg = argv[1];
215 window_t *main_window = window_open(argv[1], true, true, "vlaunch");
216 if (!main_window) {
217 printf("Cannot open main window.\n");
218 return 1;
219 }
220
221 pixel_t grd_bg = PIXEL(255, 255, 255, 255);
222
223 pixel_t btn_bg = PIXEL(255, 255, 255, 255);
224 pixel_t btn_fg = PIXEL(255, 186, 186, 186);
225 pixel_t btn_text = PIXEL(255, 0, 0, 0);
226
227 pixel_t lbl_bg = PIXEL(255, 255, 255, 255);
228 pixel_t lbl_text = PIXEL(255, 0, 0, 0);
229
230 canvas_t *logo_canvas = create_canvas(NULL, LOGO_WIDTH, LOGO_HEIGHT,
231 logo);
232 label_t *lbl_caption = create_label(NULL, "Launch application:", 16,
233 lbl_bg, lbl_text);
234 button_t *btn_vterm = create_button(NULL, "vterm", 16, btn_bg,
235 btn_fg, btn_text);
236 button_t *btn_vdemo = create_button(NULL, "vdemo", 16, btn_bg,
237 btn_fg, btn_text);
238 button_t *btn_vlaunch = create_button(NULL, "vlaunch", 16, btn_bg,
239 btn_fg, btn_text);
240 grid_t *grid = create_grid(window_root(main_window), 1, 5, grd_bg);
241
242 if ((!logo_canvas) || (!lbl_caption) || (!btn_vterm) ||
243 (!btn_vdemo) || (!btn_vlaunch) || (!grid)) {
244 window_close(main_window);
245 printf("Cannot create widgets.\n");
246 return 1;
247 }
248
249 sig_connect(&btn_vterm->clicked, NULL, on_vterm);
250 sig_connect(&btn_vdemo->clicked, NULL, on_vdemo);
251 sig_connect(&btn_vlaunch->clicked, NULL, on_vlaunch);
252
253 grid->add(grid, &logo_canvas->widget, 0, 0, 1, 1);
254 grid->add(grid, &lbl_caption->widget, 0, 1, 1, 1);
255 grid->add(grid, &btn_vterm->widget, 0, 2, 1, 1);
256 grid->add(grid, &btn_vdemo->widget, 0, 3, 1, 1);
257 grid->add(grid, &btn_vlaunch->widget, 0, 4, 1, 1);
258
259 window_resize(main_window, 0, 0, 210, 130 + LOGO_HEIGHT,
260 WINDOW_PLACEMENT_RIGHT | WINDOW_PLACEMENT_TOP);
261 window_exec(main_window);
262
263 fibril_timer_set(timer, PERIOD, timer_callback, NULL);
264
265 task_retval(0);
266 async_manager();
267
268 return 0;
269}
270
271/** @}
272 */
Note: See TracBrowser for help on using the repository browser.