source: mainline/uspace/lib/ui/src/pbutton.c@ 453f9645

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 453f9645 was 174be87, checked in by Jiri Svoboda <jiri@…>, 3 years ago

Prevent using button after free

When a button is released (after being pressed), it generates two
callbacks, 'clicked' and 'up'. If the handler of the former
closes/destroys the dialog, the second event is delivered to an
already destroyed/freed dialog.

By delivering 'up' first and 'clicked' second, we can prevent this
particular problem. Is it the right solution? Not sure.

  • Property mode set to 100644
File size: 13.9 KB
Line 
1/*
2 * Copyright (c) 2022 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 libui
30 * @{
31 */
32/**
33 * @file Push button
34 *
35 * Push button either uses text as decoration, or it can use a caller-provided
36 * function to paint the decoration.
37 */
38
39#include <errno.h>
40#include <gfx/color.h>
41#include <gfx/context.h>
42#include <gfx/render.h>
43#include <gfx/text.h>
44#include <io/pos_event.h>
45#include <stdlib.h>
46#include <str.h>
47#include <ui/control.h>
48#include <ui/paint.h>
49#include <ui/pbutton.h>
50#include "../private/pbutton.h"
51#include "../private/resource.h"
52
53/** Caption movement when button is pressed down */
54enum {
55 ui_pb_press_dx = 1,
56 ui_pb_press_dy = 1
57};
58
59static void ui_pbutton_ctl_destroy(void *);
60static errno_t ui_pbutton_ctl_paint(void *);
61static ui_evclaim_t ui_pbutton_ctl_pos_event(void *, pos_event_t *);
62
63/** Push button control ops */
64ui_control_ops_t ui_pbutton_ops = {
65 .destroy = ui_pbutton_ctl_destroy,
66 .paint = ui_pbutton_ctl_paint,
67 .pos_event = ui_pbutton_ctl_pos_event
68};
69
70/** Create new push button.
71 *
72 * @param resource UI resource
73 * @param caption Caption
74 * @param rpbutton Place to store pointer to new push button
75 * @return EOK on success, ENOMEM if out of memory
76 */
77errno_t ui_pbutton_create(ui_resource_t *resource, const char *caption,
78 ui_pbutton_t **rpbutton)
79{
80 ui_pbutton_t *pbutton;
81 errno_t rc;
82
83 pbutton = calloc(1, sizeof(ui_pbutton_t));
84 if (pbutton == NULL)
85 return ENOMEM;
86
87 rc = ui_control_new(&ui_pbutton_ops, (void *) pbutton,
88 &pbutton->control);
89 if (rc != EOK) {
90 free(pbutton);
91 return rc;
92 }
93
94 pbutton->caption = str_dup(caption);
95 if (pbutton->caption == NULL) {
96 ui_control_delete(pbutton->control);
97 free(pbutton);
98 return ENOMEM;
99 }
100
101 pbutton->res = resource;
102 *rpbutton = pbutton;
103 return EOK;
104}
105
106/** Destroy push button.
107 *
108 * @param pbutton Push button or @c NULL
109 */
110void ui_pbutton_destroy(ui_pbutton_t *pbutton)
111{
112 if (pbutton == NULL)
113 return;
114
115 ui_control_delete(pbutton->control);
116 free(pbutton);
117}
118
119/** Get base control from push button.
120 *
121 * @param pbutton Push button
122 * @return Control
123 */
124ui_control_t *ui_pbutton_ctl(ui_pbutton_t *pbutton)
125{
126 return pbutton->control;
127}
128
129/** Set push button callbacks.
130 *
131 * @param pbutton Push button
132 * @param cb Push button callbacks
133 * @param arg Callback argument
134 */
135void ui_pbutton_set_cb(ui_pbutton_t *pbutton, ui_pbutton_cb_t *cb, void *arg)
136{
137 pbutton->cb = cb;
138 pbutton->arg = arg;
139}
140
141/** Set push button decoration ops.
142 *
143 * @param pbutton Push button
144 * @param ops Push button decoration callbacks
145 * @param arg Decoration ops argument
146 */
147void ui_pbutton_set_decor_ops(ui_pbutton_t *pbutton,
148 ui_pbutton_decor_ops_t *ops, void *arg)
149{
150 pbutton->decor_ops = ops;
151 pbutton->decor_arg = arg;
152}
153
154/** Set push button flag.s
155 *
156 * @param pbutton Push button
157 * @param flags Flags
158 */
159void ui_pbutton_set_flags(ui_pbutton_t *pbutton, ui_pbutton_flags_t flags)
160{
161 pbutton->flags = flags;
162}
163
164/** Set button rectangle.
165 *
166 * @param pbutton Button
167 * @param rect New button rectangle
168 */
169void ui_pbutton_set_rect(ui_pbutton_t *pbutton, gfx_rect_t *rect)
170{
171 pbutton->rect = *rect;
172}
173
174/** Set default flag.
175 *
176 * Default button is the one activated by Enter key, it is marked
177 * by thicker frame.
178 *
179 * @param pbutton Button
180 * @param isdefault @c true iff button is default
181 */
182void ui_pbutton_set_default(ui_pbutton_t *pbutton, bool isdefault)
183{
184 pbutton->isdefault = isdefault;
185}
186
187/** Paint outer button frame.
188 *
189 * @param pbutton Push button
190 * @return EOK on success or an error code
191 */
192static errno_t ui_pbutton_paint_frame(ui_pbutton_t *pbutton)
193{
194 gfx_rect_t rect;
195 gfx_coord_t thickness;
196 errno_t rc;
197
198 thickness = pbutton->isdefault ? 2 : 1;
199
200 rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_frame_color);
201 if (rc != EOK)
202 goto error;
203
204 rect.p0.x = pbutton->rect.p0.x + 1;
205 rect.p0.y = pbutton->rect.p0.y;
206 rect.p1.x = pbutton->rect.p1.x - 1;
207 rect.p1.y = pbutton->rect.p0.y + thickness;
208 rc = gfx_fill_rect(pbutton->res->gc, &rect);
209 if (rc != EOK)
210 goto error;
211
212 rect.p0.x = pbutton->rect.p0.x + 1;
213 rect.p0.y = pbutton->rect.p1.y - thickness;
214 rect.p1.x = pbutton->rect.p1.x - 1;
215 rect.p1.y = pbutton->rect.p1.y;
216 rc = gfx_fill_rect(pbutton->res->gc, &rect);
217 if (rc != EOK)
218 goto error;
219
220 rect.p0.x = pbutton->rect.p0.x;
221 rect.p0.y = pbutton->rect.p0.y + 1;
222 rect.p1.x = pbutton->rect.p0.x + thickness;
223 rect.p1.y = pbutton->rect.p1.y - 1;
224 rc = gfx_fill_rect(pbutton->res->gc, &rect);
225 if (rc != EOK)
226 goto error;
227
228 rect.p0.x = pbutton->rect.p1.x - thickness;
229 rect.p0.y = pbutton->rect.p0.y + 1;
230 rect.p1.x = pbutton->rect.p1.x;
231 rect.p1.y = pbutton->rect.p1.y - 1;
232 rc = gfx_fill_rect(pbutton->res->gc, &rect);
233 if (rc != EOK)
234 goto error;
235
236 return EOK;
237error:
238 return rc;
239}
240
241/** Paint outset button bevel.
242 *
243 * @param pbutton Push button
244 * @return EOK on success or an error code
245 */
246static errno_t ui_pbutton_paint_outset(ui_pbutton_t *pbutton,
247 gfx_rect_t *rect)
248{
249 return ui_paint_bevel(pbutton->res->gc, rect,
250 pbutton->res->btn_highlight_color,
251 pbutton->res->btn_shadow_color, 2, NULL);
252}
253
254/** Paint inset button bevel.
255 *
256 * @param pbutton Push button
257 * @return EOK on success or an error code
258 */
259static errno_t ui_pbutton_paint_inset(ui_pbutton_t *pbutton,
260 gfx_rect_t *rect)
261{
262 return ui_paint_bevel(pbutton->res->gc, rect,
263 pbutton->res->btn_shadow_color,
264 pbutton->res->btn_face_color, 2, NULL);
265}
266
267/** Paint button text shadow.
268 *
269 * @param pbutton Push button
270 * @return EOK on success or an error code
271 */
272static errno_t ui_pbutton_paint_text_shadow(ui_pbutton_t *pbutton)
273{
274 gfx_rect_t rect;
275 errno_t rc;
276
277 rect.p0.x = pbutton->rect.p0.x + 1;
278 rect.p0.y = pbutton->rect.p0.y + 1;
279 rect.p1.x = pbutton->rect.p1.x;
280 rect.p1.y = pbutton->rect.p1.y;
281
282 rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_shadow_color);
283 if (rc != EOK)
284 goto error;
285
286 rc = gfx_fill_rect(pbutton->res->gc, &rect);
287 if (rc != EOK)
288 goto error;
289
290 return EOK;
291error:
292 return rc;
293}
294
295/** Paint push button in graphic mode.
296 *
297 * @param pbutton Push button
298 * @return EOK on success or an error code
299 */
300static errno_t ui_pbutton_paint_gfx(ui_pbutton_t *pbutton)
301{
302 gfx_coord2_t pos;
303 gfx_text_fmt_t fmt;
304 gfx_rect_t rect;
305 gfx_coord_t thickness;
306 bool depressed;
307 errno_t rc;
308
309 thickness = pbutton->isdefault ? 2 : 1;
310 depressed = pbutton->held && pbutton->inside;
311
312 rect.p0.x = pbutton->rect.p0.x + thickness;
313 rect.p0.y = pbutton->rect.p0.y + thickness;
314 rect.p1.x = pbutton->rect.p1.x - thickness;
315 rect.p1.y = pbutton->rect.p1.y - thickness;
316
317 rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_face_color);
318 if (rc != EOK)
319 goto error;
320
321 rc = gfx_fill_rect(pbutton->res->gc, &rect);
322 if (rc != EOK)
323 goto error;
324
325 /* Center of button rectangle */
326 pos.x = (rect.p0.x + rect.p1.x) / 2;
327 pos.y = (rect.p0.y + rect.p1.y) / 2;
328
329 if (depressed) {
330 pos.x += ui_pb_press_dx;
331 pos.y += ui_pb_press_dy;
332 }
333
334 if (pbutton->decor_ops != NULL && pbutton->decor_ops->paint != NULL) {
335 /* Custom decoration */
336 rc = pbutton->decor_ops->paint(pbutton, pbutton->decor_arg,
337 &pos);
338 if (rc != EOK)
339 goto error;
340 } else {
341 /* Text decoration */
342 gfx_text_fmt_init(&fmt);
343 fmt.font = pbutton->res->font;
344 fmt.color = pbutton->res->btn_text_color;
345 fmt.halign = gfx_halign_center;
346 fmt.valign = gfx_valign_center;
347
348 rc = gfx_puttext(&pos, &fmt, pbutton->caption);
349 if (rc != EOK)
350 goto error;
351 }
352
353 rc = ui_pbutton_paint_frame(pbutton);
354 if (rc != EOK)
355 goto error;
356
357 if (depressed) {
358 rc = ui_pbutton_paint_inset(pbutton, &rect);
359 if (rc != EOK)
360 goto error;
361 } else {
362 rc = ui_pbutton_paint_outset(pbutton, &rect);
363 if (rc != EOK)
364 goto error;
365 }
366
367 rc = gfx_update(pbutton->res->gc);
368 if (rc != EOK)
369 goto error;
370
371 return EOK;
372error:
373 return rc;
374}
375
376/** Paint push button in text mode.
377 *
378 * @param pbutton Push button
379 * @return EOK on success or an error code
380 */
381static errno_t ui_pbutton_paint_text(ui_pbutton_t *pbutton)
382{
383 gfx_coord2_t pos;
384 gfx_text_fmt_t fmt;
385 gfx_rect_t rect;
386 bool depressed;
387 errno_t rc;
388
389 if ((pbutton->flags & ui_pbf_no_text_depress) == 0)
390 depressed = pbutton->held && pbutton->inside;
391 else
392 depressed = false;
393
394 rc = gfx_set_color(pbutton->res->gc, pbutton->res->wnd_face_color);
395 if (rc != EOK)
396 goto error;
397
398 rc = gfx_fill_rect(pbutton->res->gc, &pbutton->rect);
399 if (rc != EOK)
400 goto error;
401
402 rect.p0.x = pbutton->rect.p0.x + (depressed ? 1 : 0);
403 rect.p0.y = pbutton->rect.p0.y;
404 rect.p1.x = pbutton->rect.p1.x - 1 + (depressed ? 1 : 0);
405 rect.p1.y = pbutton->rect.p0.y + 1;
406
407 rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_face_color);
408 if (rc != EOK)
409 goto error;
410
411 rc = gfx_fill_rect(pbutton->res->gc, &rect);
412 if (rc != EOK)
413 goto error;
414
415 /* Center of button rectangle */
416 pos.x = (rect.p0.x + rect.p1.x) / 2;
417 pos.y = (rect.p0.y + rect.p1.y) / 2;
418
419 gfx_text_fmt_init(&fmt);
420 fmt.font = pbutton->res->font;
421 fmt.color = pbutton->res->btn_text_color;
422 fmt.halign = gfx_halign_center;
423 fmt.valign = gfx_valign_center;
424
425 rc = gfx_puttext(&pos, &fmt, pbutton->caption);
426 if (rc != EOK)
427 goto error;
428
429 if (!depressed) {
430 rc = ui_pbutton_paint_text_shadow(pbutton);
431 if (rc != EOK)
432 goto error;
433 }
434
435 rc = gfx_update(pbutton->res->gc);
436 if (rc != EOK)
437 goto error;
438
439 return EOK;
440error:
441 return rc;
442}
443
444/** Paint push button.
445 *
446 * @param pbutton Push button
447 * @return EOK on success or an error code
448 */
449errno_t ui_pbutton_paint(ui_pbutton_t *pbutton)
450{
451 if (pbutton->res->textmode)
452 return ui_pbutton_paint_text(pbutton);
453 else
454 return ui_pbutton_paint_gfx(pbutton);
455}
456
457/** Press down button.
458 *
459 * @param pbutton Push button
460 */
461void ui_pbutton_press(ui_pbutton_t *pbutton)
462{
463 if (pbutton->held)
464 return;
465
466 pbutton->inside = true;
467 pbutton->held = true;
468 (void) ui_pbutton_paint(pbutton);
469 ui_pbutton_down(pbutton);
470}
471
472/** Release button.
473 *
474 * @param pbutton Push button
475 */
476void ui_pbutton_release(ui_pbutton_t *pbutton)
477{
478 if (!pbutton->held)
479 return;
480
481 pbutton->held = false;
482 ui_pbutton_up(pbutton);
483
484 if (pbutton->inside) {
485 (void) ui_pbutton_paint(pbutton);
486 ui_pbutton_clicked(pbutton);
487 }
488}
489
490/** Pointer entered button.
491 *
492 * @param pbutton Push button
493 */
494void ui_pbutton_enter(ui_pbutton_t *pbutton)
495{
496 if (pbutton->inside)
497 return;
498
499 pbutton->inside = true;
500 if (pbutton->held)
501 (void) ui_pbutton_paint(pbutton);
502}
503
504/** Pointer left button.
505 *
506 * @param pbutton Push button
507 */
508void ui_pbutton_leave(ui_pbutton_t *pbutton)
509{
510 if (!pbutton->inside)
511 return;
512
513 pbutton->inside = false;
514 if (pbutton->held)
515 (void) ui_pbutton_paint(pbutton);
516}
517
518/** Send button clicked event.
519 *
520 * @param pbutton Push button
521 */
522void ui_pbutton_clicked(ui_pbutton_t *pbutton)
523{
524 if (pbutton->cb != NULL && pbutton->cb->clicked != NULL)
525 pbutton->cb->clicked(pbutton, pbutton->arg);
526}
527
528/** Send button down event.
529 *
530 * @param pbutton Push button
531 */
532void ui_pbutton_down(ui_pbutton_t *pbutton)
533{
534 if (pbutton->cb != NULL && pbutton->cb->down != NULL)
535 pbutton->cb->down(pbutton, pbutton->arg);
536}
537
538/** Send button up event.
539 *
540 * @param pbutton Push button
541 */
542void ui_pbutton_up(ui_pbutton_t *pbutton)
543{
544 if (pbutton->cb != NULL && pbutton->cb->up != NULL)
545 pbutton->cb->up(pbutton, pbutton->arg);
546}
547
548/** Handle push button position event.
549 *
550 * @param pbutton Push button
551 * @param pos_event Position event
552 * @return @c ui_claimed iff the event is claimed
553 */
554ui_evclaim_t ui_pbutton_pos_event(ui_pbutton_t *pbutton, pos_event_t *event)
555{
556 gfx_coord2_t pos;
557 bool inside;
558
559 pos.x = event->hpos;
560 pos.y = event->vpos;
561
562 inside = gfx_pix_inside_rect(&pos, &pbutton->rect);
563
564 switch (event->type) {
565 case POS_PRESS:
566 if (inside) {
567 ui_pbutton_press(pbutton);
568 return ui_claimed;
569 }
570 break;
571 case POS_RELEASE:
572 if (pbutton->held) {
573 ui_pbutton_release(pbutton);
574 return ui_claimed;
575 }
576 break;
577 case POS_UPDATE:
578 if (inside && !pbutton->inside) {
579 ui_pbutton_enter(pbutton);
580 return ui_claimed;
581 } else if (!inside && pbutton->inside) {
582 ui_pbutton_leave(pbutton);
583 }
584 break;
585 case POS_DCLICK:
586 break;
587 }
588
589 return ui_unclaimed;
590}
591
592/** Destroy push button control.
593 *
594 * @param arg Argument (ui_pbutton_t *)
595 */
596void ui_pbutton_ctl_destroy(void *arg)
597{
598 ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
599
600 ui_pbutton_destroy(pbutton);
601}
602
603/** Paint push button control.
604 *
605 * @param arg Argument (ui_pbutton_t *)
606 * @return EOK on success or an error code
607 */
608errno_t ui_pbutton_ctl_paint(void *arg)
609{
610 ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
611
612 return ui_pbutton_paint(pbutton);
613}
614
615/** Handle push button control position event.
616 *
617 * @param arg Argument (ui_pbutton_t *)
618 * @param pos_event Position event
619 * @return @c ui_claimed iff the event is claimed
620 */
621ui_evclaim_t ui_pbutton_ctl_pos_event(void *arg, pos_event_t *event)
622{
623 ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
624
625 return ui_pbutton_pos_event(pbutton, event);
626}
627
628/** @}
629 */
Note: See TracBrowser for help on using the repository browser.