aRts audio server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

185 lines
4.4 KiB

  1. /*
  2. Copyright (C) 1997 Mark Donohoe (donohoe@kde.org)
  3. Copyright (C) 2000 Stefan Westerfeld (stefan@space.twc.de)
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. /*
  18. * This is based on Mark Donohoe's nice KRootProp class (found in
  19. * tdelibs/tdecore/krootprop.cpp). I couldn't use KRootProp directly, since
  20. * I wanted aRts (and even it's X11 support) to work without KDE and/or Qt
  21. * installed at all.
  22. */
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <X11/Xlib.h>
  27. #include <X11/Xatom.h>
  28. #include "x11globalcomm.h"
  29. #include "debug.h"
  30. #include <iostream>
  31. using namespace std;
  32. using namespace Arts;
  33. class X11GlobalComm_impl : virtual public X11GlobalComm_skel {
  34. protected:
  35. Display *X11display;
  36. Window rootWin;
  37. Atom atom;
  38. map<string,string> propertyMap;
  39. bool init;
  40. void warn(const char *what)
  41. {
  42. arts_warning("X11GlobalComm: %s", what);
  43. }
  44. public:
  45. X11GlobalComm_impl() :rootWin(0), atom(0)
  46. {
  47. X11display = XOpenDisplay(NULL);
  48. if(!X11display)
  49. {
  50. warn("Can't connect to the XServer - Initial references won't work.");
  51. init = false;
  52. }
  53. else
  54. {
  55. rootWin = DefaultRootWindow(X11display);
  56. atom = XInternAtom( X11display, "MCOPGLOBALS" , False);
  57. init = true;
  58. }
  59. }
  60. ~X11GlobalComm_impl()
  61. {
  62. if(X11display)
  63. XCloseDisplay(X11display);
  64. }
  65. void read()
  66. {
  67. if(!init) return;
  68. Atom type;
  69. int format;
  70. unsigned long nitems;
  71. unsigned long bytes_after;
  72. long offset;
  73. unsigned char *buf;
  74. propertyMap.clear();
  75. // If a property has already been opened write
  76. // the dictionary back to the root window
  77. string s;
  78. offset = 0; bytes_after = 1;
  79. while (bytes_after != 0)
  80. {
  81. XGetWindowProperty( X11display, rootWin, atom, offset, 256,
  82. False, XA_STRING, &type, &format, &nitems, &bytes_after,
  83. &buf);
  84. if(type == None) // no property -> no contents
  85. return;
  86. s += (char*) buf;
  87. offset += nitems/4;
  88. if (buf)
  89. XFree((char*) buf);
  90. }
  91. // Parse through the property string stripping out key value pairs
  92. // and putting them in the dictionary
  93. while(s.length() >0 )
  94. {
  95. // parse the string for first key-value pair separator '\n'
  96. int i = s.find("\n",0);
  97. // extract the key-values pair and remove from string
  98. string keypair = s.substr(0,i);
  99. s = s.substr(i+1,s.size()-(i+1));
  100. // split key and value and add to dictionary
  101. i = keypair.find( "=" );
  102. if( i != -1 )
  103. {
  104. string key = keypair.substr( 0, i );
  105. string value = keypair.substr( i+1, keypair.size()-(i+1) );
  106. propertyMap[key] = value;
  107. }
  108. }
  109. }
  110. void write()
  111. {
  112. if(!init) return;
  113. // bring map in the variable=value form with \n as seperator
  114. string propString;
  115. map<string,string>::iterator i = propertyMap.begin();
  116. for(i = propertyMap.begin(); i != propertyMap.end(); i++)
  117. propString += i->first + "=" + i->second + "\n";
  118. // send to the X11 server
  119. XChangeProperty( X11display, rootWin, atom,
  120. XA_STRING, 8, PropModeReplace,
  121. (const unsigned char *)propString.c_str(),
  122. propString.size()+1);
  123. XFlush(X11display);
  124. }
  125. bool put(const string& variable, const string& value)
  126. {
  127. if(!init) return false;
  128. read();
  129. if(propertyMap[variable].empty())
  130. {
  131. propertyMap[variable] = value;
  132. write();
  133. return true;
  134. }
  135. return false;
  136. }
  137. string get(const string& variable)
  138. {
  139. if(!init) return "";
  140. read();
  141. return propertyMap[variable];
  142. }
  143. void erase(const string& variable)
  144. {
  145. if(!init) return;
  146. read();
  147. propertyMap.erase(variable);
  148. write();
  149. }
  150. };
  151. REGISTER_IMPLEMENTATION(X11GlobalComm_impl);