TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
qdevicewatcher_wince.cpp
Go to the documentation of this file.
1 #include "qdevicewatcher.h"
2 #include "qdevicewatcher_p.h"
3 
4 #ifdef Q_OS_WINCE
5 #include <msgqueue.h>
6 #include <pnp.h>
7 #include <Storemgr.h> //BLOCK_DRIVER_GUID
8 #include <QtCore/QCoreApplication>
9 
10 #ifdef UNICODE
11 # if TTK_QT_VERSION_CHECK(4,0,0)
12 # define TCHAR2QString(x) QString::fromUtf16(x)
13 # else
14 # define TCHAR2QString(x) QString::fromUcs2(x)
15 # endif
16 #else
17 # define TCHAR2QString(x) QString::fromLocal8Bit((x))
18 #endif //UNICODE
19 
20 typedef union {
21  DEVDETAIL d;
22  char pad[sizeof(DEVDETAIL) + MAX_DEVCLASS_NAMELEN]; //BYTE pad[sizeof(DEVDETAIL) + (MAX_PATH * sizeof(TCHAR))];
23 } Q_DEVDETAIL;
24 
25 
27 {
28 }
29 
31 {
32  if (!initialize())
33  return false;
34  this->QThread::start();
35  return true;
36 }
37 
38 
40 {
41  quit();
42 
43  if (!StopDeviceNotifications(mNotificationHandle)) {
44  qWarning("StopDeviceNotifications() error: %d", GetLastError());
45  return false;
46  }
47  if (!CloseMsgQueue(mQueueHandle)) {
48  qWarning("CloseMsgQueue() error: %d", GetLastError());
49  return false;
50  }
51  return true;
52 }
53 
54 
56 {
57  MSGQUEUEOPTIONS msgopts;
58 
59  msgopts.dwSize = sizeof(MSGQUEUEOPTIONS);
60  msgopts.dwFlags = 0; //MSGQUEUE_VARIABLESIZE | MSGQUEUE_MSGSIZE; /*msdn*/ //MSGQUEUE_NOPRECOMMIT | MSGQUEUE_ALLOW_BROKEN
61  msgopts.dwMaxMessages = 0;
62  msgopts.cbMaxMessage = sizeof(Q_DEVDETAIL);
63  msgopts.bReadAccess = TRUE;
64  msgopts.dwMaxMessages = 0; //?
65  //msgopts.cbMaxMsgQueue = 0; //?
66  //msgopts.dwDesiredAccess = GENERIC_READ;
67  //msgopts.dwShareMode = 0;
68  //msgopts.dwCreationDisposition = CREATE_NEW;
69 
70  mQueueHandle = CreateMsgQueue(nullptr, &msgopts);
71  if (mQueueHandle == nullptr) {
72  qWarning("CreateMsgQueue() error: %d", GetLastError());
73  return false;
74  }
75  mNotificationHandle = RequestDeviceNotifications(&BLOCK_DRIVER_GUID, mQueueHandle, TRUE);
76  if (mNotificationHandle == nullptr) {
77  qWarning("RequestDeviceNotifications() error: %d", GetLastError());
78  return false;
79  }
80  return true;
81 }
82 
83 void QDeviceWatcherPrivate::run()
84 {
85  DWORD flags;
86  DWORD size;
87  Q_DEVDETAIL detail;
88  SetLastError(0); //?
89  while (true) {
90  if(WaitForSingleObject(mQueueHandle, 3000) == WAIT_OBJECT_0) {
91  while(ReadMsgQueue(mQueueHandle, &detail, sizeof(detail), &size, 1, &flags)) {
92  QString dev = TCHAR2QString(detail.d.szName);
93  QDeviceChangeEvent *event = 0;
94  if (detail.d.fAttached) {
95  emitDeviceAdded(dev);
97  } else {
98  emitDeviceRemoved(dev);
100  }
101  if (event != 0 && !event_receivers.isEmpty()) {
102  for(QObject *obj : qAsConst(event_receivers)) {
103  QCoreApplication::postEvent(obj, event, Qt::HighEventPriority);
104  }
105  }
106  }
107  }
108  }
109 }
110 
112 {
113 }
114 
115 #endif //Q_OS_WINCE
flags
Definition: http_parser.h:135
void emitDeviceAdded(const QString &dev)
voidpf void uLong size
Definition: ioapi.h:136
#define qAsConst
Definition: ttkqtglobal.h:53
The class of the device watcher event.
void emitDeviceRemoved(const QString &dev)