A thousand cuts..
[jamaendo] / jamaui / ossohelper.py
1 # ossohelper.py - Helper to osso functions
2 #
3 #  Copyright (c) 2008 INdT - Instituto Nokia de Tecnologia
4 #
5 #  This file is part of carman-python.
6 #  Modified for inclusion in Panucci (June 2009).
7 #
8 #  carman-python 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 3 of the License, or
11 #  (at your option) any later version.
12 #
13 #  carman-python 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, see <http://www.gnu.org/licenses/>.
20
21 import os
22 import logging
23
24 __log = logging.getLogger(__name__)
25
26 try:
27     import osso
28     __osso_imported__ = True
29 except ImportError:
30     __log.warning('osso module not found - are you running on a desktop?')
31     __osso_imported__ = False
32
33 DEVICE_STATE_NORMAL     = "normal"
34 OSSO_DEVSTATE_MODE_FILE = "/tmp/.libosso_device_mode_cache"
35
36 __osso_context__ = None
37 __osso_application__ = None
38 __osso_device_state__ = None
39
40 def application_init(app_name, app_version):
41     """
42     Osso application init.
43     """
44
45     global __osso_context__, __osso_device_state__, __osso_application__
46
47     if __osso_imported__:
48         if has_osso():
49             __log.warning('osso application was already called. Ignoring...')
50             return
51         try:
52             __osso_context__ = osso.Context(app_name, app_version, False)
53         except Exception, err:
54             __log.warning('osso module found but could not be initialized: %s',
55                           err)
56             __osso_context__ = None
57             return
58
59         try:
60             __osso_application__ = osso.Application(__osso_context__)
61         except Exception, err:
62             __log.warning('error creating osso application: %s' % err)
63             __osso_application__ = None
64
65         __log.info( 'osso application init sent - %s v%s', app_name,
66                     app_version)
67         __osso_device_state__ = osso.DeviceState(__osso_context__)
68 # application_init
69
70 def application_exit():
71     """
72     Osso application exit.
73     """
74     if __osso_application__ is not None and __osso_context__ is not None:
75         try:
76             __osso_application__.close()
77             __osso_context__.close()
78         except Exception, err:
79             __log.warning('application end could not be sent: %s' % err)
80         __log.info('osso application end sent')
81 # application_exit
82
83 def application_top(app_name):
84     """
85     Osso application top.
86     """
87     if __osso_imported__ and __osso_application__:
88         try:
89             __osso_application__.application_top(app_name)
90         except Exception, err:
91             __log.warning( "Error calling application top for %s: %s",
92                            app_name, err)
93         __log.info('osso application top for %s sent', app_name)
94
95 # application_top
96
97 def has_osso():
98     """
99     Return if the osso module was initialized and all objects were created
100     without any problem
101     """
102     return __osso_imported__ and not None in ( __osso_context__, 
103                                                __osso_device_state__, 
104                                                __osso_application__ )
105 # has_osso
106
107 def display_on():
108     """
109     Turn on the display
110     """
111     if __osso_device_state__ is not None:
112         __osso_device_state__.display_state_on()
113         __osso_device_state__.display_blanking_pause()
114         __log.info('osso display on')
115 # display_on
116
117 def display_blanking_pause():
118     """
119     Keep the backlight on. Should be called every 45 seconds.
120     """
121     if __osso_device_state__ is not None:
122         __osso_device_state__.display_blanking_pause()
123         __log.debug('osso blanking screen')
124 #display_blanking_pause
125
126 def get_device_state():
127     if __osso_device_state__ is not None:
128         cache_file_name = OSSO_DEVSTATE_MODE_FILE + "-" + str(os.getuid())
129         try:
130             state = os.readlink(cache_file_name)
131         except:
132             state = None
133         if not state:
134             __log.debug( "Failure to read device state from %s",
135                          cache_file_name)
136             state = DEVICE_STATE_NORMAL
137         return state
138     else:
139         return DEVICE_STATE_NORMAL