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 |
|
---|
69 | static char *winreg = NULL;
|
---|
70 | static fibril_timer_t *timer = NULL;
|
---|
71 | static list_t led_devs;
|
---|
72 |
|
---|
73 | static 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 |
|
---|
83 | static unsigned int color = 0;
|
---|
84 |
|
---|
85 | typedef struct {
|
---|
86 | link_t link;
|
---|
87 | service_id_t svc_id;
|
---|
88 | async_sess_t *sess;
|
---|
89 | } led_dev_t;
|
---|
90 |
|
---|
91 | static int app_launch(const char *app)
|
---|
92 | {
|
---|
93 | printf("%s: Spawning %s %s \n", NAME, app, winreg);
|
---|
94 |
|
---|
95 | task_id_t id;
|
---|
96 | task_wait_t wait;
|
---|
97 | int rc = task_spawnl(&id, &wait, app, app, winreg, NULL);
|
---|
98 | if (rc != EOK) {
|
---|
99 | printf("%s: Error spawning %s %s (%s)\n", NAME, app,
|
---|
100 | winreg, str_error(rc));
|
---|
101 | return -1;
|
---|
102 | }
|
---|
103 |
|
---|
104 | task_exit_t texit;
|
---|
105 | int retval;
|
---|
106 | rc = task_wait(&wait, &texit, &retval);
|
---|
107 | if ((rc != EOK) || (texit != TASK_EXIT_NORMAL)) {
|
---|
108 | printf("%s: Error retrieving retval from %s (%s)\n", NAME,
|
---|
109 | app, str_error(rc));
|
---|
110 | return -1;
|
---|
111 | }
|
---|
112 |
|
---|
113 | return retval;
|
---|
114 | }
|
---|
115 |
|
---|
116 | static void on_vterm(widget_t *widget, void *data)
|
---|
117 | {
|
---|
118 | app_launch("/app/vterm");
|
---|
119 | }
|
---|
120 |
|
---|
121 | static void on_vdemo(widget_t *widget, void *data)
|
---|
122 | {
|
---|
123 | app_launch("/app/vdemo");
|
---|
124 | }
|
---|
125 |
|
---|
126 | static void on_vlaunch(widget_t *widget, void *data)
|
---|
127 | {
|
---|
128 | app_launch("/app/vlaunch");
|
---|
129 | }
|
---|
130 |
|
---|
131 | static void timer_callback(void *data)
|
---|
132 | {
|
---|
133 | pixel_t next_color = colors[color];
|
---|
134 |
|
---|
135 | color++;
|
---|
136 | if (color >= COLORS)
|
---|
137 | color = 0;
|
---|
138 |
|
---|
139 | list_foreach(led_devs, link, led_dev_t, dev) {
|
---|
140 | if (dev->sess)
|
---|
141 | led_dev_color_set(dev->sess, next_color);
|
---|
142 | }
|
---|
143 |
|
---|
144 | fibril_timer_set(timer, PERIOD, timer_callback, NULL);
|
---|
145 | }
|
---|
146 |
|
---|
147 | static void loc_callback(void)
|
---|
148 | {
|
---|
149 | category_id_t led_cat;
|
---|
150 | int rc = loc_category_get_id("led", &led_cat, IPC_FLAG_BLOCKING);
|
---|
151 | if (rc != EOK)
|
---|
152 | return;
|
---|
153 |
|
---|
154 | service_id_t *svcs;
|
---|
155 | size_t count;
|
---|
156 | rc = loc_category_get_svcs(led_cat, &svcs, &count);
|
---|
157 | if (rc != EOK)
|
---|
158 | return;
|
---|
159 |
|
---|
160 | for (size_t i = 0; i < count; i++) {
|
---|
161 | bool known = false;
|
---|
162 |
|
---|
163 | /* Determine whether we already know this device. */
|
---|
164 | list_foreach(led_devs, link, led_dev_t, dev) {
|
---|
165 | if (dev->svc_id == svcs[i]) {
|
---|
166 | known = true;
|
---|
167 | break;
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | if (!known) {
|
---|
172 | led_dev_t *dev = (led_dev_t *) calloc(1, sizeof(led_dev_t));
|
---|
173 | if (!dev)
|
---|
174 | continue;
|
---|
175 |
|
---|
176 | link_initialize(&dev->link);
|
---|
177 | dev->svc_id = svcs[i];
|
---|
178 | dev->sess = loc_service_connect(EXCHANGE_SERIALIZE, svcs[i], 0);
|
---|
179 |
|
---|
180 | list_append(&dev->link, &led_devs);
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | // FIXME: Handle LED device removal
|
---|
185 |
|
---|
186 | free(svcs);
|
---|
187 | }
|
---|
188 |
|
---|
189 | int main(int argc, char *argv[])
|
---|
190 | {
|
---|
191 | if (argc < 2) {
|
---|
192 | printf("Compositor server not specified.\n");
|
---|
193 | return 1;
|
---|
194 | }
|
---|
195 |
|
---|
196 | list_initialize(&led_devs);
|
---|
197 | int rc = loc_register_cat_change_cb(loc_callback);
|
---|
198 | if (rc != EOK) {
|
---|
199 | printf("Unable to register callback for device discovery.\n");
|
---|
200 | return 1;
|
---|
201 | }
|
---|
202 |
|
---|
203 | timer = fibril_timer_create(NULL);
|
---|
204 | if (!timer) {
|
---|
205 | printf("Unable to create timer.\n");
|
---|
206 | return 1;
|
---|
207 | }
|
---|
208 |
|
---|
209 | surface_t *logo = decode_tga((void *) helenos_tga, helenos_tga_size, 0);
|
---|
210 | if (!logo) {
|
---|
211 | printf("Unable to decode logo.\n");
|
---|
212 | return 1;
|
---|
213 | }
|
---|
214 |
|
---|
215 | winreg = argv[1];
|
---|
216 | window_t *main_window = window_open(argv[1], true, true, "vlaunch");
|
---|
217 | if (!main_window) {
|
---|
218 | printf("Cannot open main window.\n");
|
---|
219 | return 1;
|
---|
220 | }
|
---|
221 |
|
---|
222 | pixel_t grd_bg = PIXEL(255, 255, 255, 255);
|
---|
223 |
|
---|
224 | pixel_t btn_bg = PIXEL(255, 255, 255, 255);
|
---|
225 | pixel_t btn_fg = PIXEL(255, 186, 186, 186);
|
---|
226 | pixel_t btn_text = PIXEL(255, 0, 0, 0);
|
---|
227 |
|
---|
228 | pixel_t lbl_bg = PIXEL(255, 255, 255, 255);
|
---|
229 | pixel_t lbl_text = PIXEL(255, 0, 0, 0);
|
---|
230 |
|
---|
231 | canvas_t *logo_canvas = create_canvas(NULL, LOGO_WIDTH, LOGO_HEIGHT,
|
---|
232 | logo);
|
---|
233 | label_t *lbl_caption = create_label(NULL, "Launch application:", 16,
|
---|
234 | lbl_bg, lbl_text);
|
---|
235 | button_t *btn_vterm = create_button(NULL, "vterm", 16, btn_bg,
|
---|
236 | btn_fg, btn_text);
|
---|
237 | button_t *btn_vdemo = create_button(NULL, "vdemo", 16, btn_bg,
|
---|
238 | btn_fg, btn_text);
|
---|
239 | button_t *btn_vlaunch = create_button(NULL, "vlaunch", 16, btn_bg,
|
---|
240 | btn_fg, btn_text);
|
---|
241 | grid_t *grid = create_grid(window_root(main_window), 1, 5, grd_bg);
|
---|
242 |
|
---|
243 | if ((!logo_canvas) || (!lbl_caption) || (!btn_vterm) ||
|
---|
244 | (!btn_vdemo) || (!btn_vlaunch) || (!grid)) {
|
---|
245 | window_close(main_window);
|
---|
246 | printf("Cannot create widgets.\n");
|
---|
247 | return 1;
|
---|
248 | }
|
---|
249 |
|
---|
250 | sig_connect(&btn_vterm->clicked, NULL, on_vterm);
|
---|
251 | sig_connect(&btn_vdemo->clicked, NULL, on_vdemo);
|
---|
252 | sig_connect(&btn_vlaunch->clicked, NULL, on_vlaunch);
|
---|
253 |
|
---|
254 | grid->add(grid, &logo_canvas->widget, 0, 0, 1, 1);
|
---|
255 | grid->add(grid, &lbl_caption->widget, 0, 1, 1, 1);
|
---|
256 | grid->add(grid, &btn_vterm->widget, 0, 2, 1, 1);
|
---|
257 | grid->add(grid, &btn_vdemo->widget, 0, 3, 1, 1);
|
---|
258 | grid->add(grid, &btn_vlaunch->widget, 0, 4, 1, 1);
|
---|
259 |
|
---|
260 | window_resize(main_window, 0, 0, 210, 130 + LOGO_HEIGHT,
|
---|
261 | WINDOW_PLACEMENT_RIGHT | WINDOW_PLACEMENT_TOP);
|
---|
262 | window_exec(main_window);
|
---|
263 |
|
---|
264 | fibril_timer_set(timer, PERIOD, timer_callback, NULL);
|
---|
265 |
|
---|
266 | task_retval(0);
|
---|
267 | async_manager();
|
---|
268 |
|
---|
269 | return 0;
|
---|
270 | }
|
---|
271 |
|
---|
272 | /** @}
|
---|
273 | */
|
---|