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.

WindowFlashingManager.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.windowflashing;
  23. import com.dmdirc.ClientModule.GlobalConfig;
  24. import com.dmdirc.actions.CoreActionType;
  25. import com.dmdirc.addons.ui_swing.MainFrame;
  26. import com.dmdirc.config.ConfigBinder;
  27. import com.dmdirc.config.ConfigBinding;
  28. import com.dmdirc.interfaces.ActionController;
  29. import com.dmdirc.interfaces.ActionListener;
  30. import com.dmdirc.interfaces.actions.ActionType;
  31. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  32. import javax.inject.Inject;
  33. import com.sun.jna.Native;
  34. import com.sun.jna.NativeLibrary;
  35. import com.sun.jna.Pointer;
  36. import com.sun.jna.platform.win32.User32;
  37. import com.sun.jna.platform.win32.WinDef;
  38. import com.sun.jna.platform.win32.WinUser;
  39. public class WindowFlashingManager implements ActionListener {
  40. /** Swing main frame. */
  41. private final MainFrame mainFrame;
  42. /** Action controller. */
  43. private final ActionController actionController;
  44. /** Config binder. */
  45. private final ConfigBinder binder;
  46. /** Cached blink rate setting. */
  47. @ConfigBinding(domain = "plugin-windowflashing", key = "blinkrate",
  48. fallbacks = {"plugin-windowflashing", "blinkratefallback"})
  49. private int blinkrate;
  50. /** Cached count setting. */
  51. @ConfigBinding(domain = "plugin-windowflashing", key = "flashcount",
  52. fallbacks = {"plugin-windowflashing", "flashcountfallback"})
  53. private int flashcount;
  54. /** Cached flash taskbar setting. */
  55. @ConfigBinding(domain = "plugin-windowflashing", key = "flashtaskbar")
  56. private boolean flashtaskbar;
  57. /** Cached flash caption setting. */
  58. @ConfigBinding(domain = "plugin-windowflashing", key = "flashcaption")
  59. private boolean flashcaption;
  60. /** Library instance. */
  61. private User32 user32;
  62. @Inject
  63. public WindowFlashingManager(@GlobalConfig final AggregateConfigProvider config,
  64. final MainFrame mainFrame, final ActionController actionController) {
  65. this.mainFrame = mainFrame;
  66. this.actionController = actionController;
  67. binder = config.getBinder();
  68. }
  69. public void onLoad() {
  70. user32 = (User32) Native.loadLibrary("user32", User32.class);
  71. binder.bind(this, WindowFlashing.class);
  72. actionController.registerListener(this, CoreActionType.CLIENT_FOCUS_GAINED);
  73. }
  74. public void onUnload() {
  75. actionController.unregisterListener(this);
  76. binder.unbind(this);
  77. user32 = null;
  78. NativeLibrary.getInstance("user32").dispose();
  79. }
  80. /**
  81. * Flashes an inactive window under windows.
  82. */
  83. public void flashWindow() {
  84. if (!mainFrame.isFocused()) {
  85. user32.FlashWindowEx(setupFlashObject());
  86. }
  87. }
  88. /**
  89. * Creates a new flash info object that starts flashing with the configured settings.
  90. */
  91. private WinUser.FLASHWINFO setupFlashObject() {
  92. final WinUser.FLASHWINFO flashInfo = new WinUser.FLASHWINFO();
  93. flashInfo.dwFlags = getFlags();
  94. flashInfo.dwTimeout = blinkrate;
  95. flashInfo.uCount = flashcount;
  96. flashInfo.hWnd = getHWND();
  97. flashInfo.cbSize = flashInfo.size();
  98. return flashInfo;
  99. }
  100. /**
  101. * Creates a new flash object that stops the flashing.
  102. */
  103. private WinUser.FLASHWINFO stopFlashObject() {
  104. final WinUser.FLASHWINFO flashInfo = new WinUser.FLASHWINFO();
  105. flashInfo.dwFlags = WinUser.FLASHW_STOP;
  106. flashInfo.dwTimeout = 0;
  107. flashInfo.uCount = 0;
  108. flashInfo.hWnd = getHWND();
  109. flashInfo.cbSize = flashInfo.size();
  110. return flashInfo;
  111. }
  112. /**
  113. * Returns the native handle object for the main frame.
  114. *
  115. * @return
  116. */
  117. private WinDef.HWND getHWND() {
  118. final WinDef.HWND hwnd = new WinDef.HWND();
  119. final Pointer pointer = Native.getWindowPointer(mainFrame);
  120. hwnd.setPointer(pointer);
  121. return hwnd;
  122. }
  123. /**
  124. * Calculates the flags for the flash object based on config settings.
  125. *
  126. * @return Flash info flags
  127. */
  128. private int getFlags() {
  129. int returnValue = 0;
  130. if (flashtaskbar) {
  131. returnValue |= WinUser.FLASHW_TRAY;
  132. }
  133. if (flashcaption) {
  134. returnValue |= WinUser.FLASHW_CAPTION;
  135. }
  136. if (flashcount >= 0) {
  137. returnValue |= WinUser.FLASHW_TIMER;
  138. } else {
  139. returnValue |= WinUser.FLASHW_TIMERNOFG;
  140. }
  141. return returnValue;
  142. }
  143. @Override
  144. public void processEvent(final ActionType type, final StringBuffer format,
  145. final Object... arguments) {
  146. if (mainFrame != null) {
  147. user32.FlashWindowEx(stopFlashObject());
  148. }
  149. }
  150. }