Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rotary.cc 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // ----------------------------------------------------------------------
  2. //
  3. // Copyright (C) 2010 Fons Adriaensen <fons@linuxaudio.org>
  4. // Modified by falkTX on Jan 2015 for inclusion in Carla
  5. //
  6. // This program is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation; either version 2 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. //
  20. // ----------------------------------------------------------------------
  21. #include <cairo/cairo.h>
  22. #include <cairo/cairo-xlib.h>
  23. #include <math.h>
  24. #include "rotary.h"
  25. namespace REV1 {
  26. int RotaryCtl::_wb_up = 4;
  27. int RotaryCtl::_wb_dn = 5;
  28. int RotaryCtl::_keymod = 0;
  29. int RotaryCtl::_button = 0;
  30. int RotaryCtl::_rcount = 0;
  31. int RotaryCtl::_rx = 0;
  32. int RotaryCtl::_ry = 0;
  33. RotaryCtl::RotaryCtl (X_window *parent,
  34. X_callback *cbobj,
  35. RotaryImg *image,
  36. int xp,
  37. int yp,
  38. int cbind) :
  39. X_window (parent,
  40. image->_x0 + xp, image->_y0 + yp,
  41. image->_dx, image->_dy,
  42. image->_backg->pixel),
  43. _cbobj (cbobj),
  44. _cbind (cbind),
  45. _image (image),
  46. _state (0),
  47. _count (0),
  48. _value (0),
  49. _angle (0)
  50. {
  51. x_add_events ( ExposureMask
  52. | Button1MotionMask | ButtonPressMask | ButtonReleaseMask);
  53. _cairo->initIfNeeded(parent->disp());
  54. _cairotype = _cairo->type;
  55. _cairosurf = _cairo->surf;
  56. }
  57. RotaryCtl::~RotaryCtl (void)
  58. {
  59. }
  60. void RotaryCtl::handle_event (XEvent *E)
  61. {
  62. switch (E->type)
  63. {
  64. case Expose:
  65. render ();
  66. break;
  67. case ButtonPress:
  68. bpress ((XButtonEvent *) E);
  69. break;
  70. case ButtonRelease:
  71. brelse ((XButtonEvent *) E);
  72. break;
  73. case MotionNotify:
  74. motion ((XMotionEvent *) E);
  75. break;
  76. default:
  77. fprintf (stderr, "RotaryCtl: event %d\n", E->type );
  78. }
  79. }
  80. void RotaryCtl::bpress (XButtonEvent *E)
  81. {
  82. int r = 0;
  83. double d;
  84. d = hypot (E->x - _image->_xref, E->y - _image->_yref);
  85. if (d > _image->_rad + 3) return;
  86. _keymod = E->state;
  87. if (E->button < 4)
  88. {
  89. _rx = E->x;
  90. _ry = E->y;
  91. _button = E->button;
  92. r = handle_button ();
  93. _rcount = _count;
  94. }
  95. else if (_button) return;
  96. else if ((int)E->button == _wb_up)
  97. {
  98. r = handle_mwheel (1);
  99. }
  100. else if ((int)E->button == _wb_dn)
  101. {
  102. r = handle_mwheel (-1);
  103. }
  104. if (r)
  105. {
  106. callback (r);
  107. render ();
  108. }
  109. }
  110. void RotaryCtl::brelse (XButtonEvent *E)
  111. {
  112. if (_button == (int)E->button)
  113. {
  114. _button = 0;
  115. callback (RELSE);
  116. }
  117. }
  118. void RotaryCtl::motion (XMotionEvent *E)
  119. {
  120. int dx, dy, r;
  121. if (_button)
  122. {
  123. _keymod = E->state;
  124. dx = E->x - _rx;
  125. dy = E->y - _ry;
  126. r = handle_motion (dx, dy);
  127. if (r)
  128. {
  129. callback (r);
  130. render ();
  131. }
  132. }
  133. }
  134. void RotaryCtl::set_state (int s)
  135. {
  136. _state = s;
  137. render ();
  138. }
  139. void RotaryCtl::render (void)
  140. {
  141. XImage *I;
  142. double a, c, r, x, y;
  143. I = _image->_image [_state];
  144. XPutImage (dpy (), win (), dgc (), I,
  145. _image->_x0, _image->_y0, 0, 0, _image->_dx, _image->_dy);
  146. cairo_xlib_surface_set_drawable (_cairosurf, win(),
  147. _image->_dx, _image->_dy);
  148. c = _image->_lncol [_state] ? 1.0 : 0.0;
  149. a = _angle * M_PI / 180;
  150. r = _image->_rad;
  151. x = _image->_xref;
  152. y = _image->_yref;
  153. cairo_new_path (_cairotype);
  154. cairo_move_to (_cairotype, x, y);
  155. x += r * sin (a);
  156. y -= r * cos (a);
  157. cairo_line_to (_cairotype, x, y);
  158. cairo_set_source_rgb (_cairotype, c, c, c);
  159. cairo_set_line_width (_cairotype, 1.8);
  160. cairo_stroke (_cairotype);
  161. }
  162. }