TTKMusicPlayer  4.2.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  QThread::start();
35  return true;
36 }
37 
39 {
40  quit();
41 
42  if (!StopDeviceNotifications(mNotificationHandle)) {
43  qWarning("StopDeviceNotifications() error: %d", GetLastError());
44  return false;
45  }
46  if (!CloseMsgQueue(mQueueHandle)) {
47  qWarning("CloseMsgQueue() error: %d", GetLastError());
48  return false;
49  }
50  return true;
51 }
52 
53 
55 {
56  MSGQUEUEOPTIONS msgopts;
57 
58  msgopts.dwSize = sizeof(MSGQUEUEOPTIONS);
59  msgopts.dwFlags = 0; //MSGQUEUE_VARIABLESIZE | MSGQUEUE_MSGSIZE; /*msdn*/ //MSGQUEUE_NOPRECOMMIT | MSGQUEUE_ALLOW_BROKEN
60  msgopts.dwMaxMessages = 0;
61  msgopts.cbMaxMessage = sizeof(Q_DEVDETAIL);
62  msgopts.bReadAccess = TRUE;
63  msgopts.dwMaxMessages = 0; //?
64  //msgopts.cbMaxMsgQueue = 0; //?
65  //msgopts.dwDesiredAccess = GENERIC_READ;
66  //msgopts.dwShareMode = 0;
67  //msgopts.dwCreationDisposition = CREATE_NEW;
68 
69  mQueueHandle = CreateMsgQueue(nullptr, &msgopts);
70  if (mQueueHandle == nullptr) {
71  qWarning("CreateMsgQueue() error: %d", GetLastError());
72  return false;
73  }
74  mNotificationHandle = RequestDeviceNotifications(&BLOCK_DRIVER_GUID, mQueueHandle, TRUE);
75  if (mNotificationHandle == nullptr) {
76  qWarning("RequestDeviceNotifications() error: %d", GetLastError());
77  return false;
78  }
79  return true;
80 }
81 
82 void QDeviceWatcherPrivate::run()
83 {
84  DWORD flags;
85  DWORD size;
86  Q_DEVDETAIL detail;
87  SetLastError(0); //?
88  while (true) {
89  if(WaitForSingleObject(mQueueHandle, 3000) == WAIT_OBJECT_0) {
90  while(ReadMsgQueue(mQueueHandle, &detail, sizeof(detail), &size, 1, &flags)) {
91  QString dev = TCHAR2QString(detail.d.szName);
92  QDeviceChangeEvent *event = 0;
93  if (detail.d.fAttached) {
94  emitDeviceAdded(dev);
96  } else {
97  emitDeviceRemoved(dev);
99  }
100  if (event != 0 && !event_receivers.isEmpty()) {
101  for(QObject *obj : qAsConst(event_receivers)) {
102  QCoreApplication::postEvent(obj, event, Qt::HighEventPriority);
103  }
104  }
105  }
106  }
107  }
108 }
109 
111 {
112 }
113 
114 #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:57
The class of the device watcher event.
void emitDeviceRemoved(const QString &dev)