UltraScan III
us_global.cpp
Go to the documentation of this file.
1 #include "us_global.h"
3 #include <QtCore>
4 #include <QTextStream>
5 
6 #ifdef Q_WS_X11
7 #include <unistd.h>
8 #endif
9 
11 {
12  valid = false;
13  deleteFlag = false;
14 
15 #ifndef WIN32
16  // Make the key specific to the uid
17  QString key = QString( "UltraScan%1" ).arg( getuid() );
18 #else
19  QString key = QString( "UltraScan" );
20 #endif
21 
22  sharedMemory.setKey( key );
23 
24  if ( sharedMemory.attach() )
25  {
26  valid = true;
27  }
28 
29  else if ( sharedMemory.error() == QSharedMemory::OutOfResources )
30  {
31  qDebug() << "Shared memory out of resources";
32  }
33 
34  else if ( sharedMemory.create( sizeof global ) )
35  {
36  valid = true;
37  set_global_position( QPoint( 50, 50 ) );
38  setPasswd( "" );
39  // Add an additional global initialization here
40  }
41 
42  else
43  {
44  qDebug( "Failure to create shared memory" );
45  qDebug() << sharedMemory.errorString();
46  }
47 }
48 
50 {
51  sharedMemory.detach();
52  if ( deleteFlag ) sharedMemory.deleteLater();
53 }
54 
55 void US_Global::set_global_position( const QPoint& p )
56 {
57  read_global();
59  write_global();
60 }
61 
63 {
64  read_global();
65  return global.current_position;
66 }
67 
68 void US_Global::setPasswd( const QString& pw )
69 {
70  read_global();
71  strncpy( global.passwd, pw.toLatin1(), sizeof global.passwd );
72  write_global();
73 }
74 
75 QString US_Global::passwd( void )
76 {
77  read_global();
78  char pw[64];
79  strncpy( pw, global.passwd, sizeof global.passwd );
80  return QString( pw );
81 }
82 
84 {
85  sharedMemory.lock();
86  char* from = (char*)sharedMemory.data();
87  memcpy( (char*)&global, from, qMin( sharedMemory.size(), (int)sizeof global ) );
88  sharedMemory.unlock();
89 }
90 
92 {
93  sharedMemory.lock();
94  char* to = (char*)sharedMemory.data();
95  memcpy( to, (char*)&global, qMin( sharedMemory.size(), (int)sizeof global ) );
96  sharedMemory.unlock();
97 }
98