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_ERASER)     &&
00179                 (inputFashion != SAX_INPUT_MOUSE)      &&
00180                 (inputFashion != SAX_INPUT_VNC)        &&
00181                 (inputFashion != SAX_INPUT_KEYBOARD)
00182         ) {
00183                 excWrongInputFashion (fashion);
00184                 qError (errorString(),EXC_WRONGINPUTFASHION);
00185                 return -1;
00186         }
00187         //====================================
00188         // determine new input device ID...
00189         //------------------------------------
00190         QString baseID ("Mouse");
00191         QString baseDriver ("mouse");
00192         if (fashion == SAX_INPUT_VNC) {
00193                 baseDriver = "rfbmouse";
00194         }
00195         QDict<QString>* data = mInput->getTablePointer (0);
00196         int newID = mInput->getCount (true) * 2 + 1;
00197         if ((data) && (! data->isEmpty())) {
00198                 baseDriver ="kbd";
00199                 if (fashion == SAX_INPUT_VNC) {
00200                         baseDriver = "rfbkeyb";
00201                 }
00202                 baseID = "Keyboard";
00203                 newID  = mInput->getCount (true) * 2;
00204         }
00205         if (! mInput -> addID (newID)) {
00206                 return -1;
00207         }
00208         //====================================
00209         // add new input device section...
00210         //------------------------------------
00211         QString newIDstring;
00212         newIDstring.sprintf ("%d",newID);
00213         mInput -> setItem ("Identifier",baseID + "[" + newIDstring + "]");
00214         mInput -> setItem ("InputFashion",fashion);
00215         //====================================
00216         // set some defaults...
00217         //------------------------------------
00218         mInput -> setItem ("Driver",baseDriver);
00219         //====================================
00220         // update server layout
00221         //------------------------------------
00222         if (baseID == "Mouse") {
00223                 QString inputDevice; QTextOStream (&inputDevice) 
00224                         << mLayout -> getItem ("InputDevice")
00225                         << ",Mouse[" << newIDstring << "]";
00226                 mLayout -> setItem ("InputDevice",inputDevice);
00227         }
00228         if (baseID == "Keyboard") {
00229                 QString inputDevice; QTextOStream (&inputDevice)
00230                         << mLayout -> getItem ("Keyboard")
00231                         << ",Keyboard[" << newIDstring << "]";
00232                 mLayout -> setItem ("Keyboard",inputDevice);
00233         }
00234         return mInput -> getCurrentID();
00235 }
00236 
00237 //====================================
00238 // removeDesktopDevice
00239 //------------------------------------
00240 int SaXManipulateDevices::removeDesktopDevice (int id) {
00241         // .../
00247         // ----
00248         if (! desktopHandlingAllowed) {
00249                 return -1;
00250         }
00251         //====================================
00252         // don't allow removing the core entry  
00253         //------------------------------------
00254         if (id <= 0) {
00255                 excInvalidArgument (id);
00256                 qError (errorString(),EXC_INVALIDARGUMENT);
00257                 return -1;
00258         }
00259         //====================================
00260         // remove desktop...
00261         //------------------------------------
00262         if (! mCard->delID (id)) {
00263                 return -1;
00264         }
00265         if (! mDesktop->delID (id)) {
00266                 return -1;
00267         }
00268         int newID = id - 1;
00269         //====================================
00270         // select previous desktop
00271         //------------------------------------
00272         mCard    -> setID (newID);
00273         mDesktop -> setID (newID);
00274         //====================================
00275     // update screen layout definition...
00276     //------------------------------------
00277         updateLayout (mDesktop->getCount());
00278         return mDesktop -> getCurrentID();
00279 }
00280 
00281 //====================================
00282 // removeInputDevice
00283 //------------------------------------
00284 int SaXManipulateDevices::removeInputDevice (int id) {
00285         // .../
00291         // ----
00292         if (! inputHandlingAllowed) {
00293                 return -1;
00294         }
00295         //====================================
00296         // don't allow removing the core entry   
00297         //------------------------------------
00298         if (id <= 1) {
00299                 excInvalidArgument (id);
00300                 qError (errorString(),EXC_INVALIDARGUMENT);
00301                 return -1;
00302         }
00303         //====================================
00304         // remove input devices...
00305         //------------------------------------
00306         if (! mInput->delID (id)) {
00307                 return -1;
00308         }
00309         //====================================
00310         // select previous input device
00311         //------------------------------------
00312         for (int i=1;i<=2;i++) {
00313                 QDict<QString>* data = mInput->getTablePointer (i);
00314                 if ((data) && (! data->isEmpty())) {
00315                         mInput->setID (i);
00316                         break;
00317                 }
00318         }
00319         //====================================
00320         // check input device type
00321         //------------------------------------
00322         bool isMouse = true;
00323         QString baseItem ("InputDevice");
00324         QString baseID   ("Mouse");
00325         if (mInput->getCurrentID() % 2 == 0) {
00326                 isMouse  = false;
00327                 baseItem = "Keyboard";
00328                 baseID   = "Keyboard";
00329         }
00330         //====================================
00331         // update server layout
00332         //------------------------------------
00333         QString result;
00334         QString IDstring;
00335         IDstring.sprintf ("%d",id);
00336         QString deviceList  = mLayout -> getItem (baseItem);
00337         QStringList optlist = QStringList::split ( ",", deviceList );
00338         for ( QStringList::Iterator
00339                 in = optlist.begin(); in != optlist.end(); ++in
00340         ) {
00341                 QString item (*in);
00342                 if (item == QString(baseID + "["+IDstring+"]")) {
00343                         continue;
00344                 }
00345                 QRegExp identifier ("\\[(.+)\\]");
00346                 int pos = identifier.search (item);
00347                 if (pos >= 0) {
00348                         int curID = identifier.cap(1).toInt();
00349                         if ( curID > id ) {
00350                                 QString newMK;
00351                                 newMK.sprintf ("%s[%d],",baseID.ascii(),curID - 2);
00352                                 result.append ( newMK );
00353                         } else {
00354                                 result.append (item+",");
00355                         }
00356                 }
00357         }
00358         result.remove (result.length()-1,result.length());
00359         mLayout -> setItem (baseItem,result);
00360         return mInput -> getCurrentID();
00361 }
00362 
00363 //====================================
00364 // updateLayout
00365 //------------------------------------
00366 void SaXManipulateDevices::updateLayout (int newID) {
00367         // .../
00373         // ----
00374         //====================================
00375         // remove current screen layout
00376         //------------------------------------
00377         for (int n=0;n<=newID;n++) {
00378                 QString idString; idString.sprintf ("%d",n);
00379                 mLayout -> removeEntry (QString("Screen:Screen["+ idString + "]"));
00380         }
00381         //====================================
00382         // check number of existing screens
00383         //------------------------------------
00384         int existing = newID;
00385         for (int n=0;n<=newID;n++) {
00386                 if (! mCard -> getTablePointer (n)) {
00387                         existing--;
00388                 }
00389         }
00390         //====================================
00391         // set standard layout if existing=0
00392         //------------------------------------
00393         if (existing == 0) {
00394                 mLayout -> setItem (
00395                         "Screen:Screen[0]","<none> <none> <none> <none>"
00396                 );
00397                 return;
00398         }
00399         //====================================
00400         // create standard horizontal layout
00401         //------------------------------------
00402         for (int n=0;n<=existing;n++) {
00403                 QString baseID,val;
00404                 baseID.sprintf ("%d",n);
00405                 QString key ("Screen:Screen[" + baseID + "]");
00406                 for (int pos=0;pos<4;pos++) {
00407                         int posID = 0;
00408                         QString idString;
00409                         switch (pos) {
00410                                 case 0:
00411                                         posID = n - 1;
00412                                 break;
00413                                 case 1:
00414                                         posID = n + 1;
00415                                 break;
00416                                 default:
00417                                         idString = "<none>";
00418                                 break;
00419                         };
00420                         if (idString.isEmpty()) {
00421                         if ((posID < 0) || (posID > newID)) {
00422                                 idString.sprintf ("<none>");
00423                         } else {
00424                                 idString.sprintf ("Screen[%d]",posID);
00425                         }
00426                         }
00427                         QTextOStream (&val)
00428                                 << val << " " << idString;
00429                 }
00430                 val = val.stripWhiteSpace();
00431                 mLayout -> setItem (key,val);
00432         }
00433 }
00434 } // end namespace

Generated on Mon Jan 7 17:25:36 2008 for libsax by  doxygen 1.4.6