[cstdstring] demise Format, replacing with StringUtils::Format
[vuplus_xbmc] / xbmc / linux / HALManager.h
1 #ifdef HAS_HAL
2 #ifndef HALMANAGER_H
3 #define HALMANAGER_H
4
5 /*
6  *      Copyright (C) 2005-2013 Team XBMC
7  *      http://xbmc.org
8  *
9  *  This Program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2, or (at your option)
12  *  any later version.
13  *
14  *  This Program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with XBMC; see the file COPYING.  If not, see
21  *  <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include "system.h"
26 #include <string.h>
27 #include <stdio.h>
28 #include <dbus/dbus.h>
29 #include <libhal.h>
30 #include <vector>
31
32 #define BYTE char
33 #include "utils/log.h"
34 #include "threads/CriticalSection.h"
35 #include "utils/StringUtils.h"
36 #include "utils/URIUtils.h"
37 #include "MediaSource.h"
38
39 class CHALDevice
40 {
41 public:
42   CStdString UDI;
43   CStdString FriendlyName;
44   CHALDevice(const char *udi) { UDI = udi; }
45 };
46
47 class CStorageDevice : public CHALDevice
48 {
49 public:
50   CStorageDevice(const char *udi) : CHALDevice(udi) { HotPlugged = false; Mounted = false; Approved = false; MountedByXBMC = false; }
51   bool MountedByXBMC;
52   bool Mounted;
53   bool Approved;
54   bool HotPlugged;
55   bool HalIgnore;
56   CStdString MountPoint;
57   CStdString Label;
58   CStdString UUID;
59   CStdString DevID;
60   int  Type;
61   CStdString FileSystem;
62
63   CStdString toString()
64   { // Not the prettiest but it's better than having to reproduce it elsewere in the code...
65     CStdString rtn, tmp1, tmp2, tmp3, tmp4;
66     if (UUID.size() > 0)
67       tmp1 = StringUtils::Format("UUID %s | ", UUID.c_str());
68     if (FileSystem.size() > 0)
69       tmp2 = StringUtils::Format("FileSystem %s | ", FileSystem.c_str());
70     if (MountPoint.size() > 0)
71       tmp3 = StringUtils::Format("Mounted on %s | ", MountPoint.c_str());
72     if (HotPlugged)
73       tmp4 = StringUtils::Format("HotPlugged YES | ");
74     else
75       tmp4 = StringUtils::Format("HotPlugged NO  | ");
76
77     if (Approved)
78       rtn = StringUtils::Format("%s%s%s%sType %i |Approved YES ", tmp1.c_str(), tmp2.c_str(), tmp3.c_str(), tmp4.c_str(), Type);
79     else
80       rtn = StringUtils::Format("%s%s%s%sType %i |Approved NO  ", tmp1.c_str(), tmp2.c_str(), tmp3.c_str(), tmp4.c_str(), Type);
81
82     return  rtn;
83   }
84   void toMediaSource(CMediaSource *share)
85   {
86     share->strPath = MountPoint;
87     if (Label.size() > 0)
88       share->strName = Label;
89     else
90     {
91       share->strName = MountPoint;
92       URIUtils::RemoveSlashAtEnd(share->strName);
93       share->strName = URIUtils::GetFileName(share->strName);
94     }
95
96     share->m_ignore = true;
97     if (HotPlugged)
98       share->m_iDriveType = CMediaSource::SOURCE_TYPE_REMOVABLE;
99     else if(strcmp(FileSystem.c_str(), "iso9660") == 0 || strcmp(FileSystem.c_str(), "udf") == 0)
100       share->m_iDriveType = CMediaSource::SOURCE_TYPE_DVD;
101     else
102       share->m_iDriveType = CMediaSource::SOURCE_TYPE_LOCAL;
103   }
104
105 };
106
107
108 class CHALManager
109 {
110 public:
111   static const char *StorageTypeToString(int DeviceType);
112   static int StorageTypeFromString(const char *DeviceString);
113   bool Update();
114
115   void Initialize();
116   CHALManager();
117   void Stop();
118   std::vector<CStorageDevice> GetVolumeDevices();
119   bool Eject(CStdString path);
120 protected:
121   DBusConnection *m_DBusSystemConnection;
122   LibHalContext  *m_Context;
123   static DBusError m_Error;
124   static bool NewMessage;
125
126
127   void UpdateDevice(const char *udi);
128   void AddDevice(const char *udi);
129   bool RemoveDevice(const char *udi);
130
131 private:
132   bool m_Notifications;
133   LibHalContext *InitializeHal();
134   bool InitializeDBus();
135   void GenerateGDL();
136
137   bool UnMount(CStorageDevice volume);
138   bool Mount(CStorageDevice *volume, CStdString mountpath);
139   void HandleNewVolume(CStorageDevice *dev);
140   static bool ApproveDevice(CStorageDevice *device);
141
142   static bool DeviceFromVolumeUdi(const char *udi, CStorageDevice *device);
143   static CCriticalSection m_lock;
144
145 #if defined(HAS_SDL_JOYSTICK)
146   bool m_bMultipleJoysticksSupport;
147 #endif
148
149   //Callbacks HAL
150   static void DeviceRemoved(LibHalContext *ctx, const char *udi);
151   static void DeviceNewCapability(LibHalContext *ctx, const char *udi, const char *capability);
152   static void DeviceLostCapability(LibHalContext *ctx, const char *udi, const char *capability);
153   static void DevicePropertyModified(LibHalContext *ctx, const char *udi, const char *key, dbus_bool_t is_removed, dbus_bool_t is_added);
154   static void DeviceCondition(LibHalContext *ctx, const char *udi, const char *condition_name, const char *condition_details);
155   static void DeviceAdded(LibHalContext *ctx, const char *udi);
156
157   //Remembered Devices
158   std::vector<CStorageDevice> m_Volumes;
159 #if defined(HAS_SDL_JOYSTICK)
160   std::vector<CHALDevice> m_Joysticks;
161 #endif
162 };
163
164 extern CHALManager g_HalManager;
165 #endif
166 #endif // HAS_HAL