| 1 |
{-# LANGUAGE ForeignFunctionInterface, EmptyDataDecls #-} |
| 2 |
-- | This module implements a TrayManager - an integral part of a |
| 3 |
-- Linux system tray widget, though it is not itself a widget. This |
| 4 |
-- package exports a single GObject (for use with gtk2hs) that |
| 5 |
-- implements the freedesktop.org system tray specification (it |
| 6 |
-- handles receiving events and translating them into convenient |
| 7 |
-- signals, along with the messy work of dealing with XEMBED). |
| 8 |
-- |
| 9 |
-- The basic usage of the object is to: |
| 10 |
-- |
| 11 |
-- 1. Instantiate the object with 'trayManagerNew' |
| 12 |
-- |
| 13 |
-- 2. Have it manage a specific screen with 'trayManagerManageScreen' |
| 14 |
-- |
| 15 |
-- 3. Set up handlers for the events exposed by the tray (e.g., 'trayIconAdded'). |
| 16 |
-- |
| 17 |
-- As an example, a functional system tray widget looks something like: |
| 18 |
-- |
| 19 |
-- > import Graphics.UI.Gtk |
| 20 |
-- > import Graphics.UI.Gtk.Misc.TrayManager |
| 21 |
-- > systrayNew = do |
| 22 |
-- > box <- hBoxNew False 5 |
| 23 |
-- > trayManager <- rayManagerNew |
| 24 |
-- > Just screen <- screenGetDefault |
| 25 |
-- > trayManagerManageScreen trayManager screen |
| 26 |
-- > on trayManager trayIconAdded $ \w -> do |
| 27 |
-- > widgetShowAll w |
| 28 |
-- > boxPackStart box w PackNatural 0 |
| 29 |
-- |
| 30 |
-- Note that the widgets made available in the event handlers are not |
| 31 |
-- shown by default; you need to explicitly show them if you want that |
| 32 |
-- (and you probably do). |
| 33 |
module Graphics.UI.Gtk.Misc.TrayManager ( |
| 34 |
-- * Types |
| 35 |
TrayManager, |
| 36 |
TrayManagerChild, |
| 37 |
TrayManagerClass, |
| 38 |
castToTrayManager, |
| 39 |
toTrayManager, |
| 40 |
gTypeTrayManager, |
| 41 |
|
| 42 |
-- * Functions |
| 43 |
trayManagerCheckRunning, |
| 44 |
trayManagerNew, |
| 45 |
trayManagerManageScreen, |
| 46 |
trayManagerGetChildTitle, |
| 47 |
|
| 48 |
-- * Signals |
| 49 |
trayIconAdded, |
| 50 |
trayIconRemoved, |
| 51 |
trayMessageSent, |
| 52 |
trayMessageCanceled, |
| 53 |
trayLostSelection |
| 54 |
) where |
| 55 |
|
| 56 |
import Graphics.UI.Gtk hiding ( after ) |
| 57 |
import Graphics.UI.Gtk.Abstract.Object ( makeNewObject ) |
| 58 |
import Graphics.UI.GtkInternals |
| 59 |
import System.Glib.GError ( failOnGError ) |
| 60 |
import System.Glib.GType |
| 61 |
import System.Glib.UTFString ( peekUTFString ) |
| 62 |
|
| 63 |
import Control.Monad ( liftM ) |
| 64 |
|
| 65 |
import Foreign |
| 66 |
import Foreign.C.String ( CString, peekCString ) |
| 67 |
import Foreign.C.Types |
| 68 |
import Foreign.ForeignPtr.Unsafe as UF |
| 69 |
import Unsafe.Coerce ( unsafeCoerce ) |
| 70 |
|
| 71 |
newtype TrayManager = TrayManager (ForeignPtr TrayManager) |
| 72 |
deriving (Eq, Ord) |
| 73 |
type TrayManagerChild = Ptr EggTrayManagerChild |
| 74 |
|
| 75 |
-- Empty data tags to classify some foreign pointers |
| 76 |
data EggTrayManager |
| 77 |
data EggTrayManagerChild |
| 78 |
|
| 79 |
foreign import ccall "egg_tray_manager_check_running" |
| 80 |
c_egg_tray_manager_check_running :: Ptr Screen -> IO CInt |
| 81 |
|
| 82 |
trayManagerCheckRunning :: Screen -> IO Bool |
| 83 |
trayManagerCheckRunning gdkScreen = do |
| 84 |
let ptrScreen = unsafeCoerce gdkScreen :: ForeignPtr Screen |
| 85 |
withForeignPtr ptrScreen $ \realPtr -> do |
| 86 |
res <- c_egg_tray_manager_check_running realPtr |
| 87 |
return (res /= 0) |
| 88 |
|
| 89 |
mkTrayManager :: (ForeignPtr TrayManager -> TrayManager, FinalizerPtr a) |
| 90 |
mkTrayManager = (TrayManager, objectUnrefFromMainloop) |
| 91 |
|
| 92 |
unTrayManager :: TrayManager -> ForeignPtr TrayManager |
| 93 |
unTrayManager (TrayManager o) = o |
| 94 |
|
| 95 |
class GObjectClass o => TrayManagerClass o |
| 96 |
toTrayManager :: TrayManagerClass o => o -> TrayManager |
| 97 |
toTrayManager = unsafeCastGObject . toGObject |
| 98 |
|
| 99 |
instance TrayManagerClass TrayManager |
| 100 |
instance GObjectClass TrayManager where |
| 101 |
toGObject = GObject . castForeignPtr . unTrayManager |
| 102 |
unsafeCastGObject = TrayManager . castForeignPtr . unGObject |
| 103 |
|
| 104 |
castToTrayManager :: GObjectClass o => o -> TrayManager |
| 105 |
castToTrayManager = castTo gTypeTrayManager "TrayManager" |
| 106 |
|
| 107 |
foreign import ccall "egg_tray_manager_get_type" |
| 108 |
c_egg_tray_manager_get_type :: CULong |
| 109 |
|
| 110 |
gTypeTrayManager :: GType |
| 111 |
gTypeTrayManager = fromIntegral c_egg_tray_manager_get_type |
| 112 |
|
| 113 |
foreign import ccall "egg_tray_manager_new" |
| 114 |
c_egg_tray_manager_new :: IO (Ptr EggTrayManager) |
| 115 |
|
| 116 |
trayManagerNew :: IO TrayManager |
| 117 |
trayManagerNew = |
| 118 |
makeNewObject mkTrayManager $ liftM (castPtr :: Ptr EggTrayManager -> Ptr TrayManager) $ |
| 119 |
c_egg_tray_manager_new |
| 120 |
-- tm <- c_egg_tray_manager_new |
| 121 |
-- return (unsafeCoerce tm) |
| 122 |
|
| 123 |
foreign import ccall "egg_tray_manager_manage_screen" |
| 124 |
c_egg_tray_manager_manage_screen :: Ptr EggTrayManager -> Ptr Screen -> IO CInt |
| 125 |
|
| 126 |
trayManagerManageScreen :: TrayManager -> Screen -> IO Bool |
| 127 |
trayManagerManageScreen trayManager screen = do |
| 128 |
let ptrManager = unsafeCoerce trayManager :: ForeignPtr EggTrayManager |
| 129 |
ptrScreen = unsafeCoerce screen :: ForeignPtr Screen |
| 130 |
res <- withForeignPtr ptrManager $ \realManager -> do |
| 131 |
withForeignPtr ptrScreen $ \realScreen -> do |
| 132 |
c_egg_tray_manager_manage_screen realManager realScreen |
| 133 |
return (res /= 0) |
| 134 |
|
| 135 |
foreign import ccall "egg_tray_manager_get_child_title" |
| 136 |
c_egg_tray_manager_get_child_title :: Ptr EggTrayManager -> Ptr EggTrayManagerChild -> IO (Ptr CChar) |
| 137 |
|
| 138 |
|
| 139 |
trayManagerGetChildTitle :: TrayManager -> TrayManagerChild -> IO String |
| 140 |
trayManagerGetChildTitle trayManager child = do |
| 141 |
let ptrManager = unsafeCoerce trayManager :: ForeignPtr EggTrayManager |
| 142 |
res <- withForeignPtr ptrManager $ \realManager -> do |
| 143 |
c_egg_tray_manager_get_child_title realManager child |
| 144 |
peekCString res |
| 145 |
|
| 146 |
-- | The signal emitted when a new tray icon is added. These are |
| 147 |
-- delivered even for systray icons that already exist when the tray |
| 148 |
-- manager is created. |
| 149 |
trayIconAdded :: (TrayManagerClass self) => Signal self (Widget -> IO ()) |
| 150 |
trayIconAdded = Signal (connect_OBJECT__NONE "tray_icon_added") |
| 151 |
|
| 152 |
-- | This signal is emitted when a tray icon is removed by its parent |
| 153 |
-- application. No action is really necessary here (the icon is |
| 154 |
-- removed without any intervention). You could do something here if |
| 155 |
-- you wanted, though. |
| 156 |
trayIconRemoved :: (TrayManagerClass self) => Signal self (Widget -> IO ()) |
| 157 |
trayIconRemoved = Signal (connect_OBJECT__NONE "tray_icon_removed") |
| 158 |
|
| 159 |
-- | This signal is emitted when the application that displayed an |
| 160 |
-- icon wants a semi-persistent notification displayed for its icon. |
| 161 |
-- The standard doesn't seem to require that these be honored. |
| 162 |
trayMessageSent :: (TrayManagerClass self) => Signal self (Widget -> String -> Int64 -> Int64 -> IO ()) |
| 163 |
trayMessageSent = Signal (connect_OBJECT_STRING_INT64_INT64__NONE "message_sent") |
| 164 |
|
| 165 |
-- | Similarly, the applciation can send this to cancel a previous |
| 166 |
-- persistent message. |
| 167 |
trayMessageCanceled :: (TrayManagerClass self) => Signal self (Widget -> Int64 -> IO ()) |
| 168 |
trayMessageCanceled = Signal (connect_OBJECT_INT64__NONE "message_canceled") |
| 169 |
|
| 170 |
-- | ?? |
| 171 |
trayLostSelection :: (TrayManagerClass self) => Signal self (IO ()) |
| 172 |
trayLostSelection = Signal (connect_NONE__NONE "lost_selection") |
| 173 |
|
| 174 |
|
| 175 |
-- Boilerplate stolen from gtk to make this library compatible. These |
| 176 |
-- functions aren't exported at all so I just copied them. |
| 177 |
|
| 178 |
-- stolen from gtk |
| 179 |
castTo :: (GObjectClass obj, GObjectClass obj') => GType -> String -> (obj -> obj') |
| 180 |
castTo gtype objTypeName obj = |
| 181 |
case toGObject obj of |
| 182 |
gobj@(GObject objFPtr) |
| 183 |
| typeInstanceIsA ((UF.unsafeForeignPtrToPtr.castForeignPtr) objFPtr) gtype |
| 184 |
-> unsafeCastGObject gobj |
| 185 |
| otherwise -> error $ "Cannot cast object to " ++ objTypeName |
| 186 |
|
| 187 |
connect_OBJECT__NONE :: |
| 188 |
(GObjectClass a', GObjectClass obj) => SignalName -> |
| 189 |
ConnectAfter -> obj -> |
| 190 |
(a' -> IO ()) -> |
| 191 |
IO (ConnectId obj) |
| 192 |
connect_OBJECT__NONE signal after obj user = |
| 193 |
connectGeneric signal after obj action |
| 194 |
where action :: Ptr GObject -> Ptr GObject -> IO () |
| 195 |
action _ obj1 = |
| 196 |
failOnGError $ |
| 197 |
makeNewGObject (GObject, objectUnrefFromMainloop) (return obj1) >>= \obj1' -> |
| 198 |
user (unsafeCastGObject obj1') |
| 199 |
|
| 200 |
connect_NONE__NONE :: |
| 201 |
GObjectClass obj => SignalName -> |
| 202 |
ConnectAfter -> obj -> |
| 203 |
(IO ()) -> |
| 204 |
IO (ConnectId obj) |
| 205 |
connect_NONE__NONE signal after obj user = |
| 206 |
connectGeneric signal after obj action |
| 207 |
where action :: Ptr GObject -> IO () |
| 208 |
action _ = |
| 209 |
failOnGError $ |
| 210 |
user |
| 211 |
|
| 212 |
connect_OBJECT_INT64__NONE :: (GObjectClass a', GObjectClass obj) |
| 213 |
=> SignalName |
| 214 |
-> ConnectAfter |
| 215 |
-> obj |
| 216 |
-> (a' -> Int64 -> IO ()) |
| 217 |
-> IO (ConnectId obj) |
| 218 |
connect_OBJECT_INT64__NONE signal after obj user = |
| 219 |
connectGeneric signal after obj action |
| 220 |
where |
| 221 |
action :: Ptr GObject -> Ptr GObject -> Int64 -> IO () |
| 222 |
action _ obj1 int2 = |
| 223 |
failOnGError $ makeNewGObject (GObject, objectUnrefFromMainloop) (return obj1) >>= \obj1' -> |
| 224 |
user (unsafeCastGObject obj1') int2 |
| 225 |
|
| 226 |
connect_OBJECT_STRING_INT64_INT64__NONE :: (GObjectClass a', GObjectClass obj) |
| 227 |
=> SignalName |
| 228 |
-> ConnectAfter |
| 229 |
-> obj |
| 230 |
-> (a' -> String -> Int64 -> Int64 -> IO ()) |
| 231 |
-> IO (ConnectId obj) |
| 232 |
connect_OBJECT_STRING_INT64_INT64__NONE signal after obj user = |
| 233 |
connectGeneric signal after obj action |
| 234 |
where |
| 235 |
action :: Ptr GObject -> Ptr GObject -> CString -> Int64 -> Int64 -> IO () |
| 236 |
action _ obj1 str2 int3 int4 = |
| 237 |
failOnGError $ makeNewGObject (GObject, objectUnrefFromMainloop) (return obj1) >>= \obj1' -> |
| 238 |
peekUTFString str2 >>= \str2' -> |
| 239 |
user (unsafeCastGObject obj1') str2' int3 int4 |