source: mainline/uspace/app/vdemo/vdemo.c@ c6f00b40

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c6f00b40 was fd11144, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Make display service argument optional

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/*
2 * Copyright (c) 2020 Jiri Svoboda
3 * Copyright (c) 2012 Petr Koupy
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup vdemo
31 * @{
32 */
33/** @file
34 */
35
36#include <stdbool.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <str.h>
40#include <io/pixel.h>
41#include <task.h>
42
43#include <window.h>
44#include <grid.h>
45#include <button.h>
46#include <label.h>
47
48#define NAME "vdemo"
49
50typedef struct my_label {
51 label_t label;
52 slot_t confirm;
53 slot_t cancel;
54} my_label_t;
55
56static void deinit_my_label(my_label_t *lbl)
57{
58 deinit_label(&lbl->label);
59}
60
61static void my_label_destroy(widget_t *widget)
62{
63 my_label_t *lbl = (my_label_t *) widget;
64
65 deinit_my_label(lbl);
66
67 free(lbl);
68}
69
70static void on_confirm(widget_t *widget, void *data)
71{
72 my_label_t *lbl = (my_label_t *) widget;
73 const char *confirmed = "Confirmed";
74 lbl->label.rewrite(&lbl->label.widget, (void *) confirmed);
75}
76
77static void on_cancel(widget_t *widget, void *data)
78{
79 my_label_t *lbl = (my_label_t *) widget;
80 const char *cancelled = "Cancelled";
81 lbl->label.rewrite(&lbl->label.widget, (void *) cancelled);
82}
83
84static bool init_my_label(my_label_t *lbl, widget_t *parent,
85 const char *caption, uint16_t points, pixel_t background, pixel_t foreground)
86{
87 lbl->confirm = on_confirm;
88 lbl->cancel = on_cancel;
89 bool initialized = init_label(&lbl->label, parent, NULL, caption,
90 points, background, foreground);
91 lbl->label.widget.destroy = my_label_destroy;
92 return initialized;
93}
94
95static my_label_t *create_my_label(widget_t *parent,
96 const char *caption, uint16_t points, pixel_t background, pixel_t foreground)
97{
98 my_label_t *lbl = (my_label_t *) malloc(sizeof(my_label_t));
99 if (!lbl) {
100 return NULL;
101 }
102
103 if (init_my_label(lbl, parent, caption, points, background, foreground)) {
104 return lbl;
105 } else {
106 free(lbl);
107 return NULL;
108 }
109}
110
111static void print_syntax(void)
112{
113 printf("Syntax: %s [-d <display>]\n", NAME);
114}
115
116int main(int argc, char *argv[])
117{
118 const char *disp_svc = DISPLAY_DEFAULT;
119 int i;
120
121 i = 1;
122 while (i < argc) {
123 if (str_cmp(argv[i], "-d") == 0) {
124 ++i;
125 if (i >= argc) {
126 printf("Argument missing.\n");
127 print_syntax();
128 return 1;
129 }
130
131 disp_svc = argv[i++];
132 } else {
133 printf("Invalid option '%s'.\n", argv[i]);
134 print_syntax();
135 return 1;
136 }
137 }
138
139 window_t *main_window = window_open(disp_svc, NULL,
140 WINDOW_MAIN | WINDOW_DECORATED | WINDOW_RESIZEABLE, "vdemo");
141 if (!main_window) {
142 printf("Cannot open main window.\n");
143 return 1;
144 }
145
146 pixel_t grd_bg = PIXEL(255, 240, 240, 240);
147
148 pixel_t btn_bg = PIXEL(255, 240, 240, 240);
149 pixel_t btn_fg = PIXEL(255, 186, 186, 186);
150 pixel_t btn_text = PIXEL(255, 0, 0, 0);
151
152 pixel_t lbl_bg = PIXEL(255, 240, 240, 240);
153 pixel_t lbl_text = PIXEL(255, 0, 0, 0);
154
155 my_label_t *lbl_action = create_my_label(NULL, "Hello there!", 16,
156 lbl_bg, lbl_text);
157 button_t *btn_confirm = create_button(NULL, NULL, "Confirm", 16,
158 btn_bg, btn_fg, btn_text);
159 button_t *btn_cancel = create_button(NULL, NULL, "Cancel", 16,
160 btn_bg, btn_fg, btn_text);
161 grid_t *grid = create_grid(window_root(main_window), NULL, 2, 2,
162 grd_bg);
163 if (!lbl_action || !btn_confirm || !btn_cancel || !grid) {
164 window_close(main_window);
165 printf("Cannot create widgets.\n");
166 return 1;
167 }
168
169 sig_connect(
170 &btn_confirm->clicked,
171 &lbl_action->label.widget,
172 lbl_action->confirm);
173 sig_connect(
174 &btn_cancel->clicked,
175 &lbl_action->label.widget,
176 lbl_action->cancel);
177
178 grid->add(grid, &lbl_action->label.widget, 0, 0, 2, 1);
179 grid->add(grid, &btn_confirm->widget, 0, 1, 1, 1);
180 grid->add(grid, &btn_cancel->widget, 1, 1, 1, 1);
181 window_resize(main_window, 0, 0, 200, 76,
182 WINDOW_PLACEMENT_CENTER);
183
184 window_exec(main_window);
185 task_retval(0);
186 async_manager();
187 return 0;
188}
189
190/** @}
191 */
Note: See TracBrowser for help on using the repository browser.