libsax 7.2

device.cpp

00001 /**************
00002 FILE          : device.cpp
00003 ***************
00004 PROJECT       : SaX2 - library interface
00005               :
00006 AUTHOR        : Marcus Schäfer <ms@suse.de>
00007               :
00008 BELONGS TO    : SaX2 - SuSE advanced X11 configuration 
00009               : 
00010               :
00011 DESCRIPTION   : native C++ class library to access SaX2
00012               : functionality. Easy to use interface for
00013               : //.../
00014               : - importing/exporting X11 configurations
00015               : - modifying/creating X11 configurations 
00016               : ---
00017               :
00018               :
00019 STATUS        : Status: Development
00020 **************/
00021 #include "device.h"
00022 
00023 namespace SaX {
00024 //====================================
00025 // Constructor...
00026 //------------------------------------
00027 SaXManipulateDevices::SaXManipulateDevices (
00028         SaXImport* desktop ,SaXImport* card ,SaXImport* layout
00029 ) {
00030         // .../
00038         // ----
00039         desktopHandlingAllowed = false;
00040         inputHandlingAllowed   = false;
00041         if ( (!desktop) || (!card) || (!layout)) {
00042                 excNullPointerArgument ();
00043                 qError (errorString(),EXC_NULLPOINTERARGUMENT);
00044                 return;
00045         }
00046         if ( desktop->getSectionID() != SAX_DESKTOP ) {
00047                 excDesktopImportBindFailed ( desktop->getSectionID() );
00048                 qError (errorString(),EXC_DESKTOPIMPORTBINDFAILED);
00049                 return;
00050         }
00051         if ( card->getSectionID() != SAX_CARD ) {
00052                 excCardImportBindFailed ( card->getSectionID() );
00053                 qError (errorString(),EXC_CARDIMPORTBINDFAILED);
00054                 return;
00055         }
00056         if ( layout->getSectionID() != SAX_LAYOUT ) {
00057                 excLayoutImportBindFailed ( layout->getSectionID() );
00058                 qError (errorString(),EXC_LAYOUTIMPORTBINDFAILED);
00059                 return;
00060         }
00061         desktopHandlingAllowed = true;
00062         mDesktop = desktop;
00063         mCard    = card;
00064         mLayout  = layout;
00065         mInput   = 0;
00066 }
00067 
00068 //====================================
00069 // Constructor...
00070 //------------------------------------
00071 SaXManipulateDevices::SaXManipulateDevices (
00072         SaXImport* input, SaXImport* layout
00073 ) {
00074         // .../
00081         // ----
00082         desktopHandlingAllowed = false;
00083         inputHandlingAllowed   = false;
00084         if ((!input) || (!layout)) {
00085                 excNullPointerArgument ();
00086                 qError (errorString(),EXC_NULLPOINTERARGUMENT);
00087                 return;
00088         }
00089         if (layout->getSectionID() != SAX_LAYOUT) {
00090                 excLayoutImportBindFailed ( layout->getSectionID() );
00091                 qError (errorString(),EXC_LAYOUTIMPORTBINDFAILED);
00092                 return;
00093         }
00094         if (
00095                 (input->getSectionID() != SAX_POINTERS) &&
00096                 (input->getSectionID() != SAX_KEYBOARD)
00097         ) {
00098                 excPointerImportBindFailed ( input->getSectionID() );
00099                 qError (errorString(),EXC_POINTERIMPORTBINDFAILED);
00100                 return;
00101         }
00102         inputHandlingAllowed = true;
00103         mInput   = input;
00104         mLayout  = layout;
00105         mDesktop = 0;
00106         mCard    = 0;
00107 }
00108 
00109 //====================================
00110 // addDesktopDevice
00111 //------------------------------------
00112 int SaXManipulateDevices::addDesktopDevice (void) {
00113         // .../
00116         // ---
00117         if (! desktopHandlingAllowed) {
00118                 return -1;
00119         }
00120         //====================================
00121         // determine new card/desktop ID...
00122         //------------------------------------
00123         int newID = mCard->getCount();
00124         if (! mCard->addID ( newID )) {
00125                 return -1;
00126         }
00127         if (! mDesktop->addID ( newID )) {
00128                 return -1;
00129         }
00130         //====================================
00131         // add new card/desktop sections...
00132         //------------------------------------
00133         QString newIDstring;
00134         newIDstring.sprintf ("%d",newID);
00135         mCard    -> setItem ("Identifier",QString("Device[" + newIDstring + "]"));
00136         mDesktop -> setItem ("Device"    ,QString("Device[" + newIDstring + "]"));
00137         mDesktop -> setItem ("Identifier",QString("Screen[" + newIDstring + "]"));
00138         mDesktop -> setItem ("Monitor"   ,QString("Monitor["+ newIDstring + "]"));
00139         //====================================
00140         // set some defaults...
00141         //------------------------------------
00142         mCard -> setItem ("Driver","fbdev");
00143         //====================================
00144         // update screen layout definition...
00145         //------------------------------------
00146         if (newID == 0) {
00147                 mLayout -> setItem ("Screen:Screen[0]","<none> <none> <none> <none>");
00148                 return mDesktop -> getCurrentID();
00149         }
00150         //====================================
00151         // update screen layout definition...
00152         //------------------------------------
00153         updateLayout (newID);
00154         return mDesktop -> getCurrentID();
00155 }
00156 
00157 //====================================
00158 // addInputDevice
00159 //------------------------------------
00160 int SaXManipulateDevices::addInputDevice (const char* fashion) {
00161         // .../
00166         // ----
00167         if (! inputHandlingAllowed) {
00168                 return -1;
00169         }
00170         //====================================
00171         // check fashion string...
00172         //------------------------------------
00173         QString inputFashion (fashion);
00174         if (
00175                 (inputFashion != SAX_INPUT_TOUCHPANEL) &&
00176                 (inputFashion != SAX_INPUT_TABLET)     &&
00177                 (inputFashion != SAX_INPUT_PEN)        &&
00178                 (inputFashion != SAX_INPUT_TOUCH)      &&
00179                 (inputFashion != SAX_INPUT_PAD)        &&
00180                 (inputFashion != SAX_INPUT_ERASER)     &&
00181                 (inputFashion != SAX_INPUT_MOUSE)      &&
00182                 (inputFashion != SAX_INPUT_VNC)        &&
00183                 (inputFashion != SAX_INPUT_KEYBOARD)
00184         ) {
00185                 excWrongInputFashion (fashion);
00186                 qError (errorString(),EXC_WRONGINPUTFASHION);
00187                 return -1;
00188         }
00189         //====================================
00190         // determine new input device ID...
00191         //------------------------------------
00192         QString baseID ("Mouse");
00193         QString baseDriver ("mouse");
00194         if (fashion == (char*)SAX_INPUT_VNC) {
00195                 baseDriver = "rfbmouse";
00196         }
00197         Q3Dict<QString>* data = mInput->getTablePointer (0);
00198         int newID = mInput->getCount (true) * 2 + 1;
00199         if ((data) && (! data->isEmpty())) {
00200                 baseDriver ="kbd";
00201                 if (fashion == (char*)SAX_INPUT_VNC) {
00202                         baseDriver = "rfbkeyb";
00203                 }
00204                 baseID = "Keyboard";
00205                 newID  = mInput->getCount (true) * 2;
00206         }
00207         if (! mInput -> addID (newID)) {
00208                 return -1;
00209         }
00210         //====================================
00211         // add new input device section...
00212         //------------------------------------
00213         QString newIDstring;
00214         newIDstring.sprintf ("%d",newID);
00215         mInput -> setItem ("Identifier",baseID + "[" + newIDstring + "]");
00216         mInput -> setItem ("InputFashion",fashion);
00217         //====================================
00218         // set some defaults...
00219         //------------------------------------
00220         mInput -> setItem ("Driver",baseDriver);
00221         //====================================
00222         // update server layout
00223         //------------------------------------
00224         if (baseID == "Mouse") {
00225                 QString inputDevice; QTextOStream (&inputDevice) 
00226                         << mLayout -> getItem ("InputDevice")
00227                         << ",Mouse[" << newIDstring << "]";
00228                 mLayout -> setItem ("InputDevice",inputDevice);
00229         }
00230         if (baseID == "Keyboard") {
00231                 QString inputDevice; QTextOStream (&inputDevice)
00232                         << mLayout -> getItem ("Keyboard")
00233                         << ",Keyboard[" << newIDstring << "]";
00234                 mLayout -> setItem ("Keyboard",inputDevice);
00235         }
00236         return mInput -> getCurrentID();
00237 }
00238 
00239 //====================================
00240 // removeDesktopDevice
00241 //------------------------------------
00242 int SaXManipulateDevices::removeDesktopDevice (int id) {
00243         // .../
00249         // ----
00250         if (! desktopHandlingAllowed) {
00251                 return -1;
00252         }
00253         //====================================
00254         // don't allow removing the core entry  
00255         //------------------------------------
00256         if (id <= 0) {
00257                 excInvalidArgument (id);
00258                 qError (errorString(),EXC_INVALIDARGUMENT);
00259                 return -1;
00260         }
00261         //====================================
00262         // remove desktop...
00263         //------------------------------------
00264         if (! mCard->delID (id)) {
00265                 return -1;
00266         }
00267         if (! mDesktop->delID (id)) {
00268                 return -1;
00269         }
00270         int newID = id - 1;
00271         //====================================
00272         // select previous desktop
00273         //------------------------------------
00274         mCard    -> setID (newID);
00275         mDesktop -> setID (newID);
00276         //====================================
00277     // update screen layout definition...
00278     //------------------------------------
00279         updateLayout (mDesktop->getCount());
00280         return mDesktop -> getCurrentID();
00281 }
00282 
00283 //====================================
00284 // removeInputDevice
00285 //------------------------------------
00286 int SaXManipulateDevices::removeInputDevice (int id) {
00287         // .../
00293         // ----
00294         if (! inputHandlingAllowed) {
00295                 return -1;
00296         }
00297         //====================================
00298         // don't allow removing the core entry   
00299         //------------------------------------
00300         if ((id <= 1) && (mInput->getCount() <= 2)) {
00301                 excInvalidArgument (id);
00302                 qError (errorString(),EXC_INVALIDARGUMENT);
00303                 return -1;
00304         }
00305         //====================================
00306         // remove input devices...
00307         //------------------------------------
00308         if (! mInput->delID (id)) {
00309                 return -1;
00310         }
00311         //====================================
00312         // select previous input device
00313         //------------------------------------
00314         for (int i=1;i<=2;i++) {
00315                 Q3Dict<QString>* data = mInput->getTablePointer (i);
00316                 if ((data) && (! data->isEmpty())) {
00317                         mInput->setID (i);
00318                         break;
00319                 }
00320         }
00321         //====================================
00322         // check input device type
00323         //------------------------------------
00324         bool isMouse = true;
00325         QString baseItem ("InputDevice");
00326         QString baseID   ("Mouse");
00327         if (mInput->getCurrentID() % 2 == 0) {
00328                 isMouse  = false;
00329                 baseItem = "Keyboard";
00330                 baseID   = "Keyboard";
00331         }
00332         //====================================
00333         // update server layout
00334         //------------------------------------
00335         QString result;
00336         QString IDstring;
00337         IDstring.sprintf ("%d",id);
00338         QString deviceList  = mLayout -> getItem (baseItem);
00339         QStringList optlist = QStringList::split ( ",", deviceList );
00340         for ( QStringList::Iterator
00341                 in = optlist.begin(); in != optlist.end(); ++in
00342         ) {
00343                 QString item (*in);
00344                 if (item == QString(baseID + "["+IDstring+"]")) {
00345                         continue;
00346                 }
00347                 QRegExp identifier ("\\[(.+)\\]");
00348                 int pos = identifier.search (item);
00349                 if (pos >= 0) {
00350                         int curID = identifier.cap(1).toInt();
00351                         if ( curID > id ) {
00352                                 QString newMK;
00353                                 newMK.sprintf ("%s[%d],",baseID.ascii(),curID - 2);
00354                                 result.append ( newMK );
00355                         } else {
00356                                 result.append (item+",");
00357                         }
00358                 }
00359         }
00360         result.remove (result.length()-1,result.length());
00361         mLayout -> setItem (baseItem,result);
00362         return mInput -> getCurrentID();
00363 }
00364 
00365 //====================================
00366 // updateLayout
00367 //------------------------------------
00368 void SaXManipulateDevices::updateLayout (int newID) {
00369         // .../
00375         // ----
00376         //====================================
00377         // remove current screen layout
00378         //------------------------------------
00379         for (int n=0;n<=newID;n++) {
00380                 QString idString; idString.sprintf ("%d",n);
00381                 mLayout -> removeEntry (QString("Screen:Screen["+ idString + "]"));
00382         }
00383         //====================================
00384         // check number of existing screens
00385         //------------------------------------
00386         int existing = newID;
00387         for (int n=0;n<=newID;n++) {
00388                 if (! mCard -> getTablePointer (n)) {
00389                         existing--;
00390                 }
00391         }
00392         //====================================
00393         // set standard layout if existing=0
00394         //------------------------------------
00395         if (existing == 0) {
00396                 mLayout -> setItem (
00397                         "Screen:Screen[0]","<none> <none> <none> <none>"
00398                 );
00399                 return;
00400         }
00401         //====================================
00402         // create standard horizontal layout
00403         //------------------------------------
00404         for (int n=0;n<=existing;n++) {
00405                 QString baseID,val;
00406                 baseID.sprintf ("%d",n);
00407                 QString key ("Screen:Screen[" + baseID + "]");
00408                 for (int pos=0;pos<4;pos++) {
00409                         int posID = 0;
00410                         QString idString;
00411                         switch (pos) {
00412                                 case 0:
00413                                         posID = n - 1;
00414                                 break;
00415                                 case 1:
00416                                         posID = n + 1;
00417                                 break;
00418                                 default:
00419                                         idString = "<none>";
00420                                 break;
00421                         };
00422                         if (idString.isEmpty()) {
00423                         if ((posID < 0) || (posID > newID)) {
00424                                 idString.sprintf ("<none>");
00425                         } else {
00426                                 idString.sprintf ("Screen[%d]",posID);
00427                         }
00428                         }
00429                         QTextOStream (&val)
00430                                 << val << " " << idString;
00431                 }
00432                 val = val.stripWhiteSpace();
00433                 mLayout -> setItem (key,val);
00434         }
00435 }
00436 } // end namespace