summaryrefslogtreecommitdiff
path: root/src/drivers/Base/Fl_Base_Pen_Events.H
blob: 2259c51aaa75fcc837325672df4dce8a67c79e2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// Definition of default Pen/Tablet event driver.
//
// Copyright 2025 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file.  If this
// file is missing or damaged, see the license at:
//
//     https://www.fltk.org/COPYING.php
//
// Please see the following page on how to report bugs and issues:
//
//     https://www.fltk.org/bugs.php
//


#ifndef FL_BASE_PEN_EVENTS_H
#define FL_BASE_PEN_EVENTS_H

#include <config.h>
#include <FL/core/pen_events.H>
#include <FL/Fl.H>


class Fl_Widget;

/* Pen event data storage.
  A second storage may be useful if the driver needs to collect pen data
  from multiple events, or if one system event can send multiple FLTK events.
*/
typedef struct Fl_Pen_EventData {
  double x;
  double y;
  double rx;
  double ry;
  double tilt_x;
  double tilt_y;
  double pressure;
  double proximity;
  double barrel_pressure;
  double twist;
  int pen_id;
  Fl_Pen_State state;
  Fl_Pen_State trigger;
} Fl_Pen_EventData;

extern Fl_Pen_EventData fl_pen_event_data;

void fl_pen_event_data_init(Fl_Pen_EventData *d);


/*
  Widgets and windows must subscribe to pen events. This is to reduce the amount
  of events sent into the widget hierarchy.

  Usually there is a pretty small number of subscribers, so looping through the
  subscriber list should not be an issue.

  All subscribers track their widget. If a widget is deleted while subscribed,
  including during event handling, the driver will remove the subscription.
  There is no need to explicitly unsubscribe.
 */
class Fl_Pen_Subscriber : public Fl_Widget_Tracker {
public:
  Fl_Pen_Subscriber(Fl_Widget *w) : Fl_Widget_Tracker(w) { }
};


/*
 Manage a list of subscribers using a simple array.
 */
class Fl_Pen_SubscriberList {
  Fl_Pen_Subscriber **items_;
  Fl_Widget **widgets_;
  int count_;
  int alloc_;
public:
  Fl_Pen_SubscriberList();
  ~Fl_Pen_SubscriberList();
  void cleanup();
  Fl_Pen_Subscriber *add(Fl_Widget *w);
  void remove(Fl_Widget *w);
  Fl_Pen_Subscriber *find(Fl_Widget *w);
  int count() const { return count_; }
  Fl_Pen_Subscriber *get(int i) const { return (i >= 0 && i < count_) ? items_[i] : 0; }
};

extern Fl_Pen_SubscriberList fl_pen_subscriber_list;
extern Fl_Pen_Subscriber *fl_pen_pushed;
extern Fl_Pen_Subscriber *fl_pen_below;

/* The base driver for calls by apps into the pen system.

  Most traffic is generated by system events. The data is then converted
  for the FLTK API and stored in fl_pen_event_data. The Pen interface then sends
  the appropriate FLTK events to the subscribers.

  This driver class manages calls from the app into FLTK, including subscriber
  management and queries for driver an pen abilities.
*/
class Fl_Pen_Driver {
public:
  Fl_Pen_Driver() { }
  virtual void subscribe(Fl_Widget* widget);
  virtual void unsubscribe(Fl_Widget* widget);
  virtual void release();
  virtual Fl_Pen_Trait traits();
  virtual Fl_Pen_Trait pen_traits(int pen_id);
};

extern Fl_Pen_Driver *fl_pen_driver;


#endif // FL_BASE_PEN_EVENTS_H