volume.app.c.mod 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* volume.app.c */
  2. /* Volume.app -- a simple volume control
  3. *
  4. * Copyright (C) 2000
  5. * Daniel Richard G. <skunk@mit.edu>,
  6. * timecop <timecop@japan.co.jp>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <getopt.h>
  29. #include <signal.h>
  30. #include <unistd.h>
  31. #include <sys/stat.h>
  32. #include <X11/X.h>
  33. #include <X11/Xlib.h>
  34. #include "common.h"
  35. #include "knob.h"
  36. #include "misc.h"
  37. #include "mixer.h"
  38. static int source = DEFAULT_SOURCE;
  39. static char *mixer_device = NULL;
  40. static bool list_sources = false;
  41. static Display *display;
  42. static int display_height;
  43. static double prev_button1_press_time = 0.0;
  44. static bool button1_pressed = false;
  45. static int mouse_drag_home_x;
  46. static int mouse_drag_home_y;
  47. static void
  48. signal_catch(int sig)
  49. {
  50. switch (sig)
  51. {
  52. case SIGUSR1:
  53. knob_turn(-0.1);
  54. break;
  55. case SIGUSR2:
  56. knob_turn(0.1);
  57. break;
  58. default:
  59. abort();
  60. break;
  61. }
  62. signal(sig, signal_catch);
  63. }
  64. static void
  65. button_press_event(XButtonEvent *event)
  66. {
  67. double button_press_time = get_current_time();
  68. switch (event->button)
  69. {
  70. /* Left click
  71. */
  72. case 1:
  73. knob_grab();
  74. if ((button_press_time - prev_button1_press_time) <= MAX_DOUBLE_CLICK_TIME)
  75. {
  76. /* Double-click
  77. */
  78. knob_toggle_mute();
  79. prev_button1_press_time = 0.0;
  80. } else
  81. system("amixer set Master unmute");
  82. prev_button1_press_time = button_press_time;
  83. button1_pressed = true;
  84. mouse_drag_home_x = event->x;
  85. mouse_drag_home_y = event->y;
  86. break;
  87. case 3:
  88. /*
  89. * Right click
  90. */
  91. /* popup menu? */
  92. break;
  93. case BUTTON_WHEEL_UP:
  94. knob_turn(0.05);
  95. break;
  96. case BUTTON_WHEEL_DOWN:
  97. knob_turn(-0.05);
  98. break;
  99. default:
  100. break;
  101. }
  102. }
  103. static void
  104. button_release_event(XButtonEvent *event)
  105. {
  106. if (event->button == 1)
  107. {
  108. knob_release();
  109. button1_pressed = false;
  110. }
  111. }
  112. static void
  113. mouse_motion_event(XMotionEvent *event)
  114. {
  115. if (button1_pressed)
  116. {
  117. if ((event->x == mouse_drag_home_x) && (event->y == mouse_drag_home_y))
  118. {
  119. /* This motion event was generated by an earlier
  120. * XWarpPointer() call, so ignore it.
  121. */
  122. return;
  123. }
  124. if (event->y != mouse_drag_home_y)
  125. {
  126. int delta_y = mouse_drag_home_y - event->y;
  127. float delta_volume = (float)delta_y / (float)display_height;
  128. knob_turn(delta_volume);
  129. }
  130. /* Keep mouse pointer on the knob. Note that this call
  131. * will generate a bogus motion event (see above)
  132. */
  133. XWarpPointer(
  134. display,
  135. None,
  136. event->window,
  137. event->x, event->y,
  138. 0, 0,
  139. mouse_drag_home_x, mouse_drag_home_y);
  140. }
  141. }
  142. #define HELP_TEXT \
  143. "Volume.app " VERSION "\n" \
  144. "usage:\n" \
  145. " -c <n> source to control [1]\n" \
  146. " (see -l option)\n" \
  147. " -d <dev> mixer device [" DEFAULT_MIXER_DEVICE "]\n" \
  148. " -h print this help\n" \
  149. " -l print list of available sound sources\n"
  150. void
  151. parse_cli_options(int argc, char **argv)
  152. {
  153. int opt;
  154. while ((opt = getopt(argc, argv, "c:hl")) != EOF)
  155. {
  156. switch (opt)
  157. {
  158. case 'c':
  159. if (optarg != NULL)
  160. source = strtod(optarg, NULL) - 1;
  161. break;
  162. case 'd':
  163. if (optarg != NULL)
  164. {
  165. if (mixer_device != NULL)
  166. free(mixer_device);
  167. mixer_device = strdup(optarg);
  168. }
  169. break;
  170. case 'h':
  171. fputs(HELP_TEXT, stdout);
  172. exit(0);
  173. break;
  174. case 'l':
  175. list_sources = true;
  176. break;
  177. default:
  178. break;
  179. }
  180. }
  181. }
  182. int
  183. main(int argc, char **argv)
  184. {
  185. char *display_name;
  186. XEvent event;
  187. int idle_tick = 0;
  188. #ifdef DEBUG
  189. fputs("**** Volume.app: debug build starting ****\n", stderr);
  190. #endif
  191. parse_cli_options(argc, argv);
  192. display_name = getenv("DISPLAY");
  193. display = XOpenDisplay(display_name);
  194. if (display == NULL)
  195. {
  196. if (display_name == NULL)
  197. fputs("Unable to open display\n", stderr);
  198. else
  199. fprintf(stderr, "Unable to open display \"%s\"\n", display_name);
  200. return EXIT_FAILURE;
  201. }
  202. XFlush(display);
  203. display_height = (float)DisplayHeight(display, DefaultScreen(display));
  204. if (mixer_device == NULL)
  205. mixer_device = strdup(DEFAULT_MIXER_DEVICE);
  206. mixer_init(mixer_device);
  207. free(mixer_device);
  208. if (list_sources)
  209. {
  210. mixer_print_sources();
  211. return EXIT_SUCCESS;
  212. }
  213. if ((source < 0) || (source >= mixer_get_source_count()))
  214. {
  215. fprintf(stderr, "Invalid source number: %d\n", source + 1);
  216. return EXIT_FAILURE;
  217. }
  218. mixer_set_source(source);
  219. knob_init(display);
  220. knob_update();
  221. signal(SIGUSR1, signal_catch);
  222. signal(SIGUSR2, signal_catch);
  223. /* Main event loop
  224. */
  225. while (true)
  226. {
  227. if (button1_pressed || (XPending(display) > 0))
  228. {
  229. XNextEvent(display, &event);
  230. switch (event.type)
  231. {
  232. case Expose:
  233. knob_redraw();
  234. break;
  235. case ButtonPress:
  236. button_press_event(&event.xbutton);
  237. idle_tick = 0;
  238. break;
  239. case ButtonRelease:
  240. button_release_event(&event.xbutton);
  241. idle_tick = 0;
  242. break;
  243. case MotionNotify:
  244. mouse_motion_event(&event.xmotion);
  245. idle_tick = 0;
  246. break;
  247. case DestroyNotify:
  248. XCloseDisplay(display);
  249. goto main_event_loop_exit;
  250. default:
  251. break;
  252. }
  253. } else {
  254. knob_update();
  255. usleep(100000);
  256. ++idle_tick;
  257. }
  258. }
  259. main_event_loop_exit:
  260. return EXIT_SUCCESS;
  261. }
  262. /* end volume.app.c */