• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tderandr
 

tderandr

  • tderandr
libtderandr.cc
1 /* libtderandr.cc - class KRandr that makes it easy to use XRandr in KDE
2  This file is part of KRandr 0.9.5
3  Copyright (C) 2010 Timothy Pearson
4  LibKRandr's homepage : http://www.trinitydesktop.org
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 
21  Send comments and bug fixes to Timothy Pearson <kb9vqf@pearsoncomputing.net>
22 
23 ***************************************************************************/
24 
25 #include <tqdir.h>
26 #include <tqtimer.h>
27 #include <tqstringlist.h>
28 
29 #include <tdelocale.h>
30 #include <tdemessagebox.h>
31 #include <tdeapplication.h>
32 
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <cmath>
36 
37 #include "libtderandr.h"
38 
39 #include <X11/extensions/dpms.h>
40 
41 // FIXME
42 // For now, just use the standalone xrandr program to apply the display settings
43 #define USE_XRANDR_PROGRAM
44 
45 // This routine is courtsey of an answer on "Stack Overflow"
46 // It takes an LSB-first int and makes it an MSB-first int (or vice versa)
47 unsigned int reverse_bits(register unsigned int x)
48 {
49  x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
50  x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
51  x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
52  x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
53  return((x >> 16) | (x << 16));
54 }
55 
56 // This routine returns the output of an arbitrary Bash command
57 TQString exec(const char * cmd) {
58  TQString bashcommand = cmd;
59  bashcommand = bashcommand.replace("\"", "\\\"");
60  bashcommand = TQString("/bin/bash -c \"%1\" 2>&1").arg(bashcommand);
61  FILE* pipe = popen(bashcommand.ascii(), "r");
62  if (!pipe) return "ERROR";
63  char buffer[128];
64  TQString result = "";
65  while(!feof(pipe)) {
66  if(fgets(buffer, 128, pipe) != NULL) {
67  result += buffer;
68  }
69  }
70  pclose(pipe);
71  result.remove(result.length(), 1);
72  return result;
73 }
74 
75 TQString capitalizeString(TQString in) {
76  return in.left(1).upper() + in.right(in.length()-1);
77 }
78 
79 TQString KRandrSimpleAPI::getIccFileName(TQString profileName, TQString screenName, TQString kde_confdir) {
80  KSimpleConfig *t_config = NULL;
81  KSimpleConfig *t_systemconfig = NULL;
82  int t_numberOfProfiles;
83  TQStringList t_cfgProfiles;
84  TQString retval;
85 
86  if ((profileName != NULL) && (profileName != "")) {
87  t_config = new KSimpleConfig( TQString::fromLatin1( "kiccconfigrc" ));
88  t_config->setGroup(NULL);
89  if (t_config->readBoolEntry("EnableICC", false) == true) {
90  t_config->setGroup(profileName);
91  retval = t_config->readEntry(screenName);
92  }
93  else {
94  retval = "";
95  }
96  delete t_config;
97  }
98  else {
99  t_systemconfig = new KSimpleConfig( kde_confdir + TQString("/kicc/kiccconfigrc") );
100  t_systemconfig->setGroup(NULL);
101  if (t_systemconfig->readBoolEntry("EnableICC", false) == true) {
102  retval = t_systemconfig->readEntry("ICCFile");
103  }
104  else {
105  retval = "";
106  }
107  delete t_systemconfig;
108  }
109 
110  return retval;
111 }
112 
113 TQString KRandrSimpleAPI::applyIccFile(TQString screenName, TQString fileName) {
114  int i;
115  int j;
116  Display *randr_display;
117  ScreenInfo *randr_screen_info;
118  XRROutputInfo *output_info;
119 
120  int screenNumber = 0;
121 
122  if (fileName != "") {
123  // FIXME
124  // This should use the RRSetCrtcGamma function when available
125  // That is the only way to get proper setting when two output are active at the same time
126  // (otherwise in clone mode only one screen is available)
127 
128  // HACK
129  // For now, simply exit with no changes if screenName is not an active output
130 
131  if (isValid() == true) {
132  screenNumber = -1;
133  randr_display = tqt_xdisplay();
134  randr_screen_info = read_screen_info(randr_display);
135  if (randr_screen_info == NULL) {
136  return "";
137  }
138  j=0;
139  for (i = 0; i < randr_screen_info->n_output; i++) {
140  output_info = randr_screen_info->outputs[i]->info;
141  // Look for ON outputs...
142  if (!randr_screen_info->outputs[i]->cur_crtc) {
143  continue;
144  }
145  // ...that are connected
146  if (RR_Disconnected == randr_screen_info->outputs[i]->info->connection) {
147  continue;
148  }
149  if (output_info->name == screenName) {
150  screenNumber = j;
151  }
152  j++;
153  }
154  freeScreenInfoStructure(randr_screen_info);
155  }
156 
157  if (screenNumber >= 0) {
158  // Apply ICC settings with XCalib
159  TQString icc_command;
160  FILE *pipe_xcalib;
161  char xcalib_result[2048];
162  int i;
163  xcalib_result[0]=0;
164 
165  icc_command = TQString("xcalib \"%1\"").arg(fileName);
166  if ((pipe_xcalib = popen(icc_command.ascii(), "r")) == NULL)
167  {
168  printf("Xcalib pipe error\n [xcalib apply]");
169  }
170  else {
171  if (fgets(xcalib_result, 2048, pipe_xcalib)) {
172  pclose(pipe_xcalib);
173  for (i=1;i<2048;i++) {
174  if (xcalib_result[i] == 0) {
175  xcalib_result[i-1]=0;
176  i=2048;
177  }
178  }
179  if (strlen(xcalib_result) > 2) {
180  return xcalib_result;
181  }
182  }
183  else {
184  return "";
185  }
186  }
187  }
188  }
189  else {
190  // Reset ICC profile on this screen
191 
192  // FIXME
193  // This should use the RRSetCrtcGamma function when available
194  // That is the only way to get proper setting when two output are active at the same time
195  // (otherwise in clone mode only one screen is available)
196 
197  // HACK
198  // For now, simply exit with no changes if screenName is not an active output
199 
200  if (isValid() == true) {
201  screenNumber = -1;
202  randr_display = tqt_xdisplay();
203  randr_screen_info = read_screen_info(randr_display);
204  if (randr_screen_info == NULL) {
205  return "";
206  }
207  j=0;
208  for (i = 0; i < randr_screen_info->n_output; i++) {
209  output_info = randr_screen_info->outputs[i]->info;
210  // Look for ON outputs...
211  if (!randr_screen_info->outputs[i]->cur_crtc) {
212  continue;
213  }
214  // ...that are connected
215  if (RR_Disconnected == randr_screen_info->outputs[i]->info->connection) {
216  continue;
217  }
218  if (output_info->name == screenName) {
219  screenNumber = j;
220  }
221  j++;
222  }
223  freeScreenInfoStructure(randr_screen_info);
224  }
225 
226  if (screenNumber >= 0) {
227  // Apply ICC settings with XCalib
228  TQString icc_command;
229  FILE *pipe_xcalib;
230  char xcalib_result[2048];
231  int i;
232  xcalib_result[0]=0;
233 
234  icc_command = TQString("xcalib -c");
235  if ((pipe_xcalib = popen(icc_command.ascii(), "r")) == NULL)
236  {
237  printf("Xcalib pipe error\n [xcalib clear]");
238  }
239  else {
240  if (fgets(xcalib_result, 2048, pipe_xcalib)) {
241  pclose(pipe_xcalib);
242  for (i=1;i<2048;i++) {
243  if (xcalib_result[i] == 0) {
244  xcalib_result[i-1]=0;
245  i=2048;
246  }
247  }
248  if (strlen(xcalib_result) > 2) {
249  return xcalib_result;
250  }
251  }
252  else {
253  return "";
254  }
255  }
256  }
257  }
258  return "";
259 }
260 
261 TQString KRandrSimpleAPI::applyIccConfiguration(TQString profileName, TQString kde_confdir) {
262  int i;
263  Display *randr_display;
264  ScreenInfo *randr_screen_info;
265  XRROutputInfo *output_info;
266  KSimpleConfig *t_config;
267 
268  int screenNumber = 0;
269  TQString errorstr = "";
270 
271  t_config = new KSimpleConfig( TQString::fromLatin1( "kiccconfigrc" ));
272 
273  // Find all screens
274  if (isValid() == true) {
275  randr_display = tqt_xdisplay();
276  randr_screen_info = read_screen_info(randr_display);
277  if (randr_screen_info == NULL) {
278  return "";
279  }
280  for (i = 0; i < randr_screen_info->n_output; i++) {
281  output_info = randr_screen_info->outputs[i]->info;
282  errorstr = applyIccFile(output_info->name, getIccFileName(profileName, output_info->name, kde_confdir));
283  if (errorstr != "") {
284  return errorstr;
285  }
286  }
287  freeScreenInfoStructure(randr_screen_info);
288  }
289  else {
290  return applyIccFile(getIccFileName(profileName, "Default", kde_confdir), "Default");
291  }
292 
293  t_config->writeEntry("CurrentProfile", profileName);
294  t_config->sync();
295  delete t_config;
296 
297  return "";
298 }
299 
300 TQString KRandrSimpleAPI::getEDIDMonitorName(int card, TQString displayname) {
301  TQString edid;
302  TQByteArray binaryedid = getEDID(card, displayname);
303  if (binaryedid.isNull())
304  return TQString();
305 
306  // Get the manufacturer ID
307  unsigned char letter_1 = ((binaryedid[8]>>2) & 0x1F) + 0x40;
308  unsigned char letter_2 = (((binaryedid[8] & 0x03) << 3) | ((binaryedid[9]>>5) & 0x07)) + 0x40;
309  unsigned char letter_3 = (binaryedid[9] & 0x1F) + 0x40;
310  TQChar qletter_1 = TQChar(letter_1);
311  TQChar qletter_2 = TQChar(letter_2);
312  TQChar qletter_3 = TQChar(letter_3);
313  TQString manufacturer_id = TQString("%1%2%3").arg(qletter_1).arg(qletter_2).arg(qletter_3);
314 
315  // Get the model ID
316  unsigned int raw_model_id = (((binaryedid[10] << 8) | binaryedid[11]) << 16) & 0xFFFF0000;
317  // Reverse the bit order
318  unsigned int model_id = reverse_bits(raw_model_id);
319 
320  // Try to get the model name
321  bool has_friendly_name = false;
322  unsigned char descriptor_block[18];
323  int i;
324  for (i=72;i<90;i++) {
325  descriptor_block[i-72] = binaryedid[i] & 0xFF;
326  }
327  if ((descriptor_block[0] != 0) || (descriptor_block[1] != 0) || (descriptor_block[3] != 0xFC)) {
328  for (i=90;i<108;i++) {
329  descriptor_block[i-90] = binaryedid[i] & 0xFF;
330  }
331  if ((descriptor_block[0] != 0) || (descriptor_block[1] != 0) || (descriptor_block[3] != 0xFC)) {
332  for (i=108;i<126;i++) {
333  descriptor_block[i-108] = binaryedid[i] & 0xFF;
334  }
335  }
336  }
337 
338  TQString monitor_name;
339  if ((descriptor_block[0] == 0) && (descriptor_block[1] == 0) && (descriptor_block[3] == 0xFC)) {
340  char* pos = strchr((char *)(descriptor_block+5), '\n');
341  if (pos) {
342  *pos = 0;
343  has_friendly_name = true;
344  monitor_name = TQString((char *)(descriptor_block+5));
345  }
346  else {
347  has_friendly_name = false;
348  }
349  }
350 
351  // [FIXME]
352  // Look up manudacturer names if possible!
353 
354  if (has_friendly_name)
355  edid = TQString("%1 %2").arg(manufacturer_id).arg(monitor_name);
356  else
357  edid = TQString("%1 0x%2").arg(manufacturer_id).arg(model_id, 0, 16);
358 
359  return edid;
360 }
361 
362 TQByteArray KRandrSimpleAPI::getEDID(int card, TQString displayname) {
363  TQFile file(TQString("/sys/class/drm/card%1-%2/edid").arg(card).arg(displayname));
364  if (!file.open (IO_ReadOnly))
365  return TQByteArray();
366  TQByteArray binaryedid = file.readAll();
367  file.close();
368  return binaryedid;
369 }
370 
371 TQString KRandrSimpleAPI::getCurrentProfile () {
372  TQString profileName;
373  KSimpleConfig *t_config;
374 
375  t_config = new KSimpleConfig( TQString::fromLatin1( "kiccconfigrc" ));
376  profileName = t_config->readEntry("CurrentProfile");
377  delete t_config;
378  return profileName;
379 }
380 
381 TQString KRandrSimpleAPI::applySystemWideIccConfiguration(TQString kde_confdir) {
382  // Apply ICC settings with XCalib
383  TQString icc_command;
384  FILE *pipe_xcalib;
385  char xcalib_result[2048];
386  int i;
387  xcalib_result[0]=0;
388 
389  icc_command = TQString("xcalib \"%1\"").arg(getIccFileName(NULL, "Default", kde_confdir));
390  if ((pipe_xcalib = popen(icc_command.ascii(), "r")) == NULL)
391  {
392  printf("Xcalib pipe error [xcalib apply]\n");
393  }
394  else {
395  if (fgets(xcalib_result, 2048, pipe_xcalib)) {
396  pclose(pipe_xcalib);
397  for (i=1;i<2048;i++) {
398  if (xcalib_result[i] == 0) {
399  xcalib_result[i-1]=0;
400  i=2048;
401  }
402  }
403  if (strlen(xcalib_result) > 2) {
404  return xcalib_result;
405  }
406  }
407  else {
408  return "";
409  }
410  }
411  return "";
412 }
413 
414 TQStringList KRandrSimpleAPI::getDisplayConfigurationProfiles(TQString kde_confdir) {
415  TQStringList ret;
416 
417  TQDir d(kde_confdir + "/displayconfig/");
418  d.setFilter(TQDir::Files);
419  d.setSorting(TQDir::Name);
420 
421  const TQFileInfoList *list = d.entryInfoList();
422  if (list) {
423  TQFileInfoListIterator it(*list);
424  TQFileInfo *fi;
425 
426  while ((fi = it.current()) != 0) {
427  if (fi->fileName() != "default") {
428  ret.append(fi->fileName());
429  }
430  ++it;
431  }
432  }
433 
434  return ret;
435 }
436 
437 bool KRandrSimpleAPI::deleteDisplayConfiguration(TQString profilename, TQString kde_confdir) {
438  TQString fileName = kde_confdir + "/displayconfig/";
439  fileName.append(profilename);
440  return (!unlink(fileName.ascii()));
441 }
442 
443 bool KRandrSimpleAPI::renameDisplayConfiguration(TQString profilename, TQString newprofilename, TQString kde_confdir) {
444  TQString fileName = kde_confdir + "/displayconfig/";
445  TQString newFileName = fileName;
446  fileName.append(profilename);
447  newFileName.append(newprofilename);
448  TQDir d(kde_confdir + "/displayconfig/");
449  return (d.rename(fileName, newFileName));
450 }
451 
452 void KRandrSimpleAPI::saveDisplayConfiguration(bool enable, bool applyonstart, TQString profilename, TQString defaultprofilename, TQString kde_confdir, TQPtrList<SingleScreenData> screenInfoArray) {
453  int i;
454 
455  TQString filename;
456 
457  filename = "displayglobals";
458  filename.prepend(kde_confdir.append("/"));
459  KSimpleConfig* display_config = new KSimpleConfig( filename );
460  display_config->setGroup("General");
461  display_config->writeEntry("EnableDisplayControl", enable);
462  display_config->writeEntry("ApplySettingsOnStart", applyonstart);
463  display_config->writeEntry("StartupProfileName", defaultprofilename);
464  display_config->sync();
465  delete display_config;
466 
467  filename = profilename;
468  if (filename == "") {
469  filename = "default";
470  }
471  filename.prepend(kde_confdir.append("/displayconfig/"));
472 
473  display_config = new KSimpleConfig( filename );
474 
475  i=0;
476  SingleScreenData *screendata;
477  for ( screendata=screenInfoArray.first(); screendata; screendata=screenInfoArray.next() ) {
478  display_config->setGroup(TQString("SCREEN %1").arg(i));
479  display_config->writeEntry("ScreenUniqueName", screendata->screenUniqueName);
480  display_config->writeEntry("ScreenFriendlyName", screendata->screenFriendlyName);
481  display_config->writeEntry("GenericScreenDetected", screendata->generic_screen_detected);
482  display_config->writeEntry("ScreenConnected", screendata->screen_connected);
483  display_config->writeEntry("Resolutions", screendata->resolutions);
484  display_config->writeEntry("RefreshRates", screendata->refresh_rates);
485  display_config->writeEntry("ColorDepths", screendata->color_depths);
486  display_config->writeEntry("AvailableRotations", screendata->rotations);
487  display_config->writeEntry("CurrentResolution", screendata->current_resolution_index);
488  display_config->writeEntry("CurrentRefreshRate", screendata->current_refresh_rate_index);
489  display_config->writeEntry("CurrentColorDepth", screendata->current_color_depth_index);
490  display_config->writeEntry("CurrentRotation", screendata->current_rotation_index);
491  display_config->writeEntry("CurrentOrientiation", screendata->current_orientation_mask);
492  display_config->writeEntry("GammaRed", screendata->gamma_red);
493  display_config->writeEntry("GammaGreen", screendata->gamma_green);
494  display_config->writeEntry("GammaBlue", screendata->gamma_blue);
495  display_config->writeEntry("CurrentXFlip", screendata->has_x_flip);
496  display_config->writeEntry("CurrentYFlip", screendata->has_y_flip);
497  display_config->writeEntry("SupportsTransformation", screendata->supports_transformations);
498  display_config->writeEntry("IsPrimary", screendata->is_primary);
499  display_config->writeEntry("IsExtended", screendata->is_extended);
500  display_config->writeEntry("AbsXPos", screendata->absolute_x_position);
501  display_config->writeEntry("AbsYPos", screendata->absolute_y_position);
502  display_config->writeEntry("CurrentXPixelCount", screendata->current_x_pixel_count);
503  display_config->writeEntry("CurrentYPixelCount", screendata->current_y_pixel_count);
504  display_config->writeEntry("HasDPMS", screendata->has_dpms);
505  display_config->writeEntry("EnableDPMS", screendata->enable_dpms);
506  display_config->writeEntry("DPMSStandbyDelay", screendata->dpms_standby_delay);
507  display_config->writeEntry("DPMSSuspendDelay", screendata->dpms_suspend_delay);
508  display_config->writeEntry("DPMSPowerDownDelay", screendata->dpms_off_delay);
509  i++;
510  }
511 
512  display_config->sync();
513  delete display_config;
514 }
515 
516 TQPoint KRandrSimpleAPI::applyStartupDisplayConfiguration(TQString kde_confdir) {
517  bool applyonstart = getDisplayConfigurationStartupAutoApplyEnabled(kde_confdir);
518  if (applyonstart) {
519  TQString profilename = getDisplayConfigurationStartupAutoApplyName(kde_confdir);
520  return applyDisplayConfiguration(profilename, kde_confdir);
521  }
522  else {
523  return TQPoint();
524  }
525 }
526 
527 TQPoint KRandrSimpleAPI::applyDisplayConfiguration(TQString profilename, TQString kde_confdir) {
528  TQPoint ret;
529 
530  bool enabled = getDisplayConfigurationEnabled(kde_confdir);
531  if (profilename == "") {
532  profilename = "default";
533  }
534 
535  if (enabled) {
536  TQPtrList<SingleScreenData> screenInfoArray;
537  screenInfoArray = loadDisplayConfiguration(profilename, kde_confdir);
538  if (screenInfoArray.count() > 0) {
539  applyDisplayConfiguration(screenInfoArray, FALSE, kde_confdir);
540  }
541  destroyScreenInformationObject(screenInfoArray);
542  screenInfoArray = readCurrentDisplayConfiguration();
543  ensureMonitorDataConsistency(screenInfoArray);
544  ret = primaryScreenOffsetFromTLC(screenInfoArray);
545  destroyScreenInformationObject(screenInfoArray);
546  }
547 
548  return ret;
549 }
550 
551 TQPtrList<SingleScreenData> KRandrSimpleAPI::loadDisplayConfiguration(TQString profilename, TQString kde_confdir) {
552  int i;
553 
554  TQString filename;
555  filename = profilename;
556  if (filename == "") {
557  filename = "default";
558  }
559  filename.prepend(kde_confdir.append("/displayconfig/"));
560 
561  KSimpleConfig* display_config = new KSimpleConfig( filename );
562 
563  TQStringList grouplist = display_config->groupList();
564  SingleScreenData *screendata;
565  TQPtrList<SingleScreenData> screenInfoArray;
566  for ( TQStringList::Iterator it = grouplist.begin(); it != grouplist.end(); ++it ) {
567  if ((*it).startsWith("SCREEN ")) {
568  display_config->setGroup(*it);
569  i = ((*it).remove("SCREEN ")).toInt();
570  screendata = new SingleScreenData;
571  screenInfoArray.append(screendata);
572  screendata->screenUniqueName = display_config->readEntry("ScreenUniqueName");
573  screendata->screenFriendlyName = display_config->readEntry("ScreenFriendlyName");
574  screendata->generic_screen_detected = display_config->readBoolEntry("GenericScreenDetected");
575  screendata->screen_connected = display_config->readBoolEntry("ScreenConnected");
576  screendata->resolutions = display_config->readListEntry("Resolutions");
577  screendata->refresh_rates = display_config->readListEntry("RefreshRates");
578  screendata->color_depths = display_config->readListEntry("ColorDepths");
579  screendata->rotations = display_config->readListEntry("AvailableRotations");
580  screendata->current_resolution_index = display_config->readNumEntry("CurrentResolution");
581  screendata->current_refresh_rate_index = display_config->readNumEntry("CurrentRefreshRate");
582  screendata->current_color_depth_index = display_config->readNumEntry("CurrentColorDepth");
583  screendata->current_rotation_index = display_config->readNumEntry("CurrentRotation");
584  screendata->current_orientation_mask = display_config->readNumEntry("CurrentOrientiation");
585  screendata->gamma_red = display_config->readDoubleNumEntry("GammaRed");
586  screendata->gamma_green = display_config->readDoubleNumEntry("GammaGreen");
587  screendata->gamma_blue = display_config->readDoubleNumEntry("GammaBlue");
588  screendata->has_x_flip = display_config->readBoolEntry("CurrentXFlip");
589  screendata->has_y_flip = display_config->readBoolEntry("CurrentYFlip");
590  screendata->supports_transformations = display_config->readBoolEntry("SupportsTransformation");
591  screendata->is_primary = display_config->readBoolEntry("IsPrimary");
592  screendata->is_extended = display_config->readBoolEntry("IsExtended");
593  screendata->absolute_x_position = display_config->readNumEntry("AbsXPos");
594  screendata->absolute_y_position = display_config->readNumEntry("AbsYPos");
595  screendata->current_x_pixel_count = display_config->readNumEntry("CurrentXPixelCount");
596  screendata->current_y_pixel_count = display_config->readNumEntry("CurrentYPixelCount");
597  screendata->has_dpms = display_config->readBoolEntry("HasDPMS");
598  screendata->enable_dpms = display_config->readBoolEntry("EnableDPMS");
599  screendata->dpms_standby_delay = display_config->readNumEntry("DPMSStandbyDelay");
600  screendata->dpms_suspend_delay = display_config->readNumEntry("DPMSSuspendDelay");
601  screendata->dpms_off_delay = display_config->readNumEntry("DPMSPowerDownDelay");
602  }
603  }
604 
605  delete display_config;
606 
607  return screenInfoArray;
608 }
609 
610 int KRandrSimpleAPI::getHardwareRotationFlags(SingleScreenData* screendata) {
611  int rotationFlags = 0;
612  TQString rotationDesired = *screendata->rotations.at(screendata->current_rotation_index);
613  if (rotationDesired == ROTATION_0_DEGREES_STRING) {
614  rotationFlags = rotationFlags | RandRScreen::Rotate0;
615  }
616  else if (rotationDesired == ROTATION_90_DEGREES_STRING) {
617  rotationFlags = rotationFlags | RandRScreen::Rotate90;
618  }
619  else if (rotationDesired == ROTATION_180_DEGREES_STRING) {
620  rotationFlags = rotationFlags | RandRScreen::Rotate180;
621  }
622  else if (rotationDesired == ROTATION_270_DEGREES_STRING) {
623  rotationFlags = rotationFlags | RandRScreen::Rotate270;
624  }
625  if (screendata->has_x_flip) {
626  rotationFlags = rotationFlags | RandRScreen::ReflectX;
627  }
628  if (screendata->has_y_flip) {
629  rotationFlags = rotationFlags | RandRScreen::ReflectY;
630  }
631  return rotationFlags;
632 }
633 
634 #define USE_XRANDR_PROGRAM
635 
636 bool KRandrSimpleAPI::applyDisplayConfiguration(TQPtrList<SingleScreenData> screenInfoArray, bool test, TQString kde_confdir) {
637  int i;
638  int j;
639  bool accepted = true;
640  Display *randr_display;
641  XRROutputInfo *output_info;
642  ScreenInfo *randr_screen_info;
643 
644  SingleScreenData *screendata;
645 
646  TQPtrList<SingleScreenData> oldconfig;
647  if (test == TRUE) {
648  oldconfig = readCurrentDisplayConfiguration();
649  }
650 
651  if (isValid() == true) {
652 #ifdef USE_XRANDR_PROGRAM
653  // Assemble the command string for xrandr
654  TQString command;
655  command = "xrandr";
656 
657  randr_display = tqt_xdisplay();
658  randr_screen_info = read_screen_info(randr_display);
659  for (i = 0; i < screenInfoArray.count(); i++) {
660  screendata = screenInfoArray.at(i);
661  if (screendata) {
662  output_info = randr_screen_info->outputs[i]->info;
663  command.append(" --output ").append(output_info->name);
664  if (screendata->is_primary || screendata->is_extended) {
665  command.append(TQString(" --mode %1x%2").arg(screendata->current_x_pixel_count).arg(screendata->current_y_pixel_count));
666  command.append(TQString(" --pos %1x%2").arg(screendata->absolute_x_position).arg(screendata->absolute_y_position));
667  command.append(TQString(" --refresh %1").arg((*screendata->refresh_rates.at(screendata->current_refresh_rate_index)).replace("Hz", "")));
668  command.append(TQString(" --gamma %1:%2:%3").arg(screendata->gamma_red).arg(screendata->gamma_green).arg(screendata->gamma_blue));
669  if (screendata->current_rotation_index == 0) command.append(" --rotate ").append("normal");
670  if (screendata->current_rotation_index == 1) command.append(" --rotate ").append("left");
671  if (screendata->current_rotation_index == 2) command.append(" --rotate ").append("inverted");
672  if (screendata->current_rotation_index == 3) command.append(" --rotate ").append("right");
673  if ((screendata->has_x_flip == 0) && (screendata->has_y_flip == 0)) command.append(" --reflect ").append("normal");
674  if ((screendata->has_x_flip == 1) && (screendata->has_y_flip == 0)) command.append(" --reflect ").append("x");
675  if ((screendata->has_x_flip == 0) && (screendata->has_y_flip == 1)) command.append(" --reflect ").append("y");
676  if ((screendata->has_x_flip == 1) && (screendata->has_y_flip == 1)) command.append(" --reflect ").append("xy");
677  if (screendata->is_primary) {
678  command.append(" --primary");
679  }
680  }
681  else {
682  command.append(" --off");
683  }
684  }
685  else {
686  printf("[WARNING] Unable to find configuration for monitor %d; settings may not be correctly applied...\n", i); fflush(stdout);
687  }
688  }
689  freeScreenInfoStructure(randr_screen_info);
690 
691  TQString xrandr_command_output = exec(command.ascii());
692  xrandr_command_output = xrandr_command_output.stripWhiteSpace();
693  if (test) {
694  if (xrandr_command_output != "") {
695  applyDisplayConfiguration(oldconfig, FALSE, kde_confdir);
696  accepted = false;
697  destroyScreenInformationObject(oldconfig);
698  KMessageBox::sorry(0, xrandr_command_output, i18n("XRandR encountered a problem"));
699  return accepted;
700  }
701  }
702 #else
703  randr_display = tqt_xdisplay();
704  randr_screen_info = read_screen_info(randr_display);
705  // Turn off all displays
706  for (i = 0; i < screenInfoArray.count(); i++) {
707  screendata = screenInfoArray.at(i);
708  output_info = randr_screen_info->outputs[i]->info;
709 
710  randr_screen_info->cur_crtc = randr_screen_info->outputs[i]->cur_crtc;
711  randr_screen_info->cur_output = randr_screen_info->outputs[i];
712  randr_screen_info->cur_output->auto_set = 0;
713  randr_screen_info->cur_output->off_set = 1;
714  output_off (randr_screen_info, randr_screen_info->cur_output);
715  j=main_low_apply(randr_screen_info);
716  }
717  freeScreenInfoStructure(randr_screen_info);
718  randr_screen_info = read_screen_info(randr_display);
719  // Turn on the primary display
720  for (i = 0; i < screenInfoArray.count(); i++) {
721  screendata = screenInfoArray.at(i);
722  output_info = randr_screen_info->outputs[i]->info;
723 
724  if (screendata->is_primary == true) {
725  randr_screen_info->cur_crtc = randr_screen_info->outputs[i]->cur_crtc;
726  randr_screen_info->cur_output = randr_screen_info->outputs[i];
727  randr_screen_info->cur_output->auto_set = 1;
728  randr_screen_info->cur_output->off_set = 0;
729  output_auto (randr_screen_info, randr_screen_info->cur_output);
730  j=main_low_apply(randr_screen_info);
731  }
732  }
733  freeScreenInfoStructure(randr_screen_info);
734  // Handle the remaining displays
735  randr_screen_info = read_screen_info(randr_display);
736  for (i = 0; i < screenInfoArray.count(); i++) {
737  screendata = screenInfoArray.at(i);
738  output_info = randr_screen_info->outputs[i]->info;
739 
740  // Activate or deactivate the screens as necessary
741  randr_screen_info->cur_crtc = randr_screen_info->outputs[i]->cur_crtc;
742  randr_screen_info->cur_output = randr_screen_info->outputs[i];
743  if (screendata->is_primary == false) {
744  if (screendata->is_primary || screendata->is_extended) {
745  randr_screen_info->cur_output->auto_set = 1;
746  randr_screen_info->cur_output->off_set = 0;
747  output_auto (randr_screen_info, randr_screen_info->cur_output);
748  j=main_low_apply(randr_screen_info);
749  }
750  else {
751  randr_screen_info->cur_output->auto_set = 0;
752  randr_screen_info->cur_output->off_set = 1;
753  output_off (randr_screen_info, randr_screen_info->cur_output);
754  j=main_low_apply(randr_screen_info);
755  }
756  }
757  }
758  freeScreenInfoStructure(randr_screen_info);
759  randr_screen_info = read_screen_info(randr_display);
760  for (i = 0; i < screenInfoArray.count(); i++) {
761  screendata = screenInfoArray.at(i);
762  output_info = randr_screen_info->outputs[i]->info;
763 
764  if (screendata->is_primary || screendata->is_extended) {
765  // Set rotation, refresh rate, and size
766  RandRScreen *cur_screen = new RandRScreen(i);
767  cur_screen->proposeSize(screendata->current_resolution_index);
768  cur_screen->proposeRefreshRate(screendata->current_refresh_rate_index);
769  cur_screen->proposeRotation(getHardwareRotationFlags(screendata));
770  cur_screen->applyProposed();
771  delete cur_screen;
772 
773  // Force data reload
774  randr_screen_info = read_screen_info(randr_display);
775  output_info = randr_screen_info->outputs[i]->info;
776 
777  // Finally, set the screen's position
778  randr_screen_info->cur_crtc = randr_screen_info->outputs[i]->cur_crtc;
779  if (randr_screen_info->cur_crtc) {
780  randr_screen_info->cur_crtc->cur_x = screendata->absolute_x_position;
781  randr_screen_info->cur_crtc->cur_y = screendata->absolute_y_position;
782  j=main_low_apply(randr_screen_info);
783  }
784  }
785  }
786  freeScreenInfoStructure(randr_screen_info);
787 #endif
788  }
789 
790  applyDisplayGamma(screenInfoArray);
791  applyDisplayDPMS(screenInfoArray);
792  TQString current_icc_profile = getCurrentProfile();
793  applySystemWideIccConfiguration(kde_confdir);
794  applyIccConfiguration(current_icc_profile, kde_confdir);
795 
796  if (test == TRUE) {
797  int ret = showTestConfigurationDialog();
798  if (!ret) {
799  applyDisplayConfiguration(oldconfig, FALSE, kde_confdir);
800  accepted = false;
801  }
802  destroyScreenInformationObject(oldconfig);
803  }
804 
805  return accepted;
806 }
807 
808 TQPtrList<SingleScreenData> KRandrSimpleAPI::copyScreenInformationObject(TQPtrList<SingleScreenData> screenInfoArray) {
809  SingleScreenData *origscreendata;
810  SingleScreenData *copyscreendata;
811  TQPtrList<SingleScreenData> retArray;
812  for ( origscreendata = screenInfoArray.first(); origscreendata; origscreendata = screenInfoArray.next() ) {
813  copyscreendata = new SingleScreenData;
814  *copyscreendata = *origscreendata;
815  retArray.append(copyscreendata);
816  }
817  return retArray;
818 }
819 
820 void KRandrSimpleAPI::destroyScreenInformationObject(TQPtrList<SingleScreenData> screenInfoArray) {
821  SingleScreenData *screendata;
822  for ( screendata = screenInfoArray.first(); screendata; screendata = screenInfoArray.next() ) {
823  screenInfoArray.remove(screendata);
824  delete screendata;
825  }
826 }
827 
828 void KRandrSimpleAPI::ensureMonitorDataConsistency(TQPtrList<SingleScreenData> screenInfoArray) {
829  int i;
830  SingleScreenData *screendata;
831 
832  int numberOfScreens = screenInfoArray.count();
833 
834  for (i=0;i<numberOfScreens;i++) {
835  screendata = screenInfoArray.at(i);
836  if (!screendata->screen_connected) {
837  screendata->is_primary = false;
838  screendata->is_extended = false;
839  }
840  }
841 
842  bool has_primary_monitor = false;
843  for (i=0;i<numberOfScreens;i++) {
844  screendata = screenInfoArray.at(i);
845  if (screendata->is_primary) {
846  has_primary_monitor = true;
847  }
848  }
849  if (!has_primary_monitor) {
850  for (i=0;i<numberOfScreens;i++) {
851  screendata = screenInfoArray.at(i);
852  if (!has_primary_monitor) {
853  if (screendata->screen_connected && screendata->is_extended) {
854  screendata->is_primary = true;
855  screendata->is_extended = true;
856  has_primary_monitor = true;
857  }
858  }
859  }
860  }
861  if (!has_primary_monitor) {
862  for (i=0;i<numberOfScreens;i++) {
863  screendata = screenInfoArray.at(i);
864  if (!has_primary_monitor) {
865  if (screendata->screen_connected) {
866  screendata->is_primary = true;
867  screendata->is_extended = true;
868  has_primary_monitor = true;
869  }
870  }
871  }
872  }
873 
874  bool found_first_primary_monitor = false;
875  for (i=0;i<numberOfScreens;i++) {
876  screendata = screenInfoArray.at(i);
877  if (screendata->is_primary) {
878  if (!found_first_primary_monitor) {
879  found_first_primary_monitor = true;
880  }
881  else {
882  screendata->is_primary = false;
883  }
884  }
885  }
886 
887  for (i=0;i<numberOfScreens;i++) {
888  screendata = screenInfoArray.at(i);
889  if (screendata->is_primary) {
890  screendata->is_extended = true;
891  }
892  }
893 
894  for (i=0;i<numberOfScreens;i++) {
895  screendata = screenInfoArray.at(i);
896  TQString resolutionstring = screendata->resolutions[screendata->current_resolution_index];
897  int separator_pos = resolutionstring.find(" x ");
898  TQString x_res_string = resolutionstring.left(separator_pos);
899  TQString y_res_string = resolutionstring.right(resolutionstring.length()-separator_pos-3);
900  screendata->current_x_pixel_count = x_res_string.toInt();
901  screendata->current_y_pixel_count = y_res_string.toInt();
902  screendata->current_orientation_mask = getHardwareRotationFlags(screendata);
903  }
904 
905  // Each screen's absolute position is given relative to the primary monitor
906  // Fix up the absolute positions
907  int primary_offset_x = 0;
908  int primary_offset_y = 0;
909  for (i=0;i<numberOfScreens;i++) {
910  screendata = screenInfoArray.at(i);
911  if (screendata->is_primary) {
912  primary_offset_x = screendata->absolute_x_position;
913  primary_offset_y = screendata->absolute_y_position;
914  primary_offset_x = primary_offset_x * (-1);
915  primary_offset_y = primary_offset_y * (-1);
916  }
917  }
918  for (i=0;i<numberOfScreens;i++) {
919  screendata = screenInfoArray.at(i);
920  screendata->absolute_x_position = screendata->absolute_x_position + primary_offset_x;
921  screendata->absolute_y_position = screendata->absolute_y_position + primary_offset_y;
922  }
923 }
924 
925 TQPoint KRandrSimpleAPI::primaryScreenOffsetFromTLC(TQPtrList<SingleScreenData> screenInfoArray) {
926  int i;
927  SingleScreenData *screendata;
928  int numberOfScreens = screenInfoArray.count();
929 
930  int primary_offset_x = 0;
931  int primary_offset_y = 0;
932  for (i=0;i<numberOfScreens;i++) {
933  screendata = screenInfoArray.at(i);
934  if (screendata->absolute_x_position < primary_offset_x) {
935  primary_offset_x = screendata->absolute_x_position;
936  }
937  if (screendata->absolute_y_position < primary_offset_y) {
938  primary_offset_y = screendata->absolute_y_position;
939  }
940  }
941  primary_offset_x = primary_offset_x * (-1);
942  primary_offset_y = primary_offset_y * (-1);
943 
944  return TQPoint(primary_offset_x, primary_offset_y);
945 }
946 
947 HotPlugRulesList KRandrSimpleAPI::getHotplugRules(TQString kde_confdir) {
948  int i;
949  TQString filename;
950  HotPlugRulesList ret;
951 
952  filename = "displayglobals";
953  filename.prepend(kde_confdir.append("/"));
954  KSimpleConfig* display_config = new KSimpleConfig( filename );
955 
956  TQStringList grouplist = display_config->groupList();
957  for ( TQStringList::Iterator it = grouplist.begin(); it != grouplist.end(); ++it ) {
958  if (!(*it).startsWith("Hotplug-Rule")) {
959  continue;
960  }
961  HotPlugRule rule;
962  display_config->setGroup(*it);
963  rule.outputs = display_config->readListEntry("Outputs");
964  rule.states = display_config->readIntListEntry("States");
965  rule.profileName = display_config->readEntry("Profile");
966  ret.append(rule);
967  }
968  delete display_config;
969 
970  return ret;
971 }
972 
973 void KRandrSimpleAPI::saveHotplugRules(HotPlugRulesList rules, TQString kde_confdir) {
974  int i;
975  TQString filename;
976 
977  filename = "displayglobals";
978  filename.prepend(kde_confdir.append("/"));
979  KSimpleConfig* display_config = new KSimpleConfig( filename );
980  TQStringList grouplist = display_config->groupList();
981  for ( TQStringList::Iterator it = grouplist.begin(); it != grouplist.end(); ++it ) {
982  if (!(*it).startsWith("Hotplug-Rule")) {
983  continue;
984  }
985  display_config->deleteGroup(*it, true, false);
986  }
987  HotPlugRulesList::Iterator it;
988  i=0;
989  for (it=rules.begin(); it != rules.end(); ++it) {
990  display_config->setGroup(TQString("Hotplug-Rule%1").arg(i));
991  display_config->writeEntry("Outputs", (*it).outputs);
992  display_config->writeEntry("States", (*it).states);
993  display_config->writeEntry("Profile", (*it).profileName);
994  i++;
995  }
996  display_config->sync();
997  delete display_config;
998 }
999 
1000 bool KRandrSimpleAPI::getDisplayConfigurationEnabled(TQString kde_confdir) {
1001  TQString filename = "displayglobals";
1002  filename.prepend(kde_confdir.append("/"));
1003  KSimpleConfig* display_config = new KSimpleConfig( filename );
1004  display_config->setGroup("General");
1005  bool enabled = display_config->readBoolEntry("EnableDisplayControl", false);
1006  delete display_config;
1007 
1008  return enabled;
1009 }
1010 
1011 bool KRandrSimpleAPI::getDisplayConfigurationStartupAutoApplyEnabled(TQString kde_confdir) {
1012  TQString filename = "displayglobals";
1013  filename.prepend(kde_confdir.append("/"));
1014  KSimpleConfig* display_config = new KSimpleConfig( filename );
1015  display_config->setGroup("General");
1016  bool applyonstart = display_config->readBoolEntry("ApplySettingsOnStart", false);
1017  delete display_config;
1018 
1019  return applyonstart;
1020 }
1021 
1022 TQString KRandrSimpleAPI::getDisplayConfigurationStartupAutoApplyName(TQString kde_confdir) {
1023  TQString filename = "displayglobals";
1024  filename.prepend(kde_confdir.append("/"));
1025  KSimpleConfig* display_config = new KSimpleConfig( filename );
1026  display_config->setGroup("General");
1027  TQString profilename = display_config->readEntry("StartupProfileName", "");
1028  delete display_config;
1029 
1030  return profilename;
1031 }
1032 
1033 void KRandrSimpleAPI::applyHotplugRules(TQString kde_confdir) {
1034  bool enabled = getDisplayConfigurationEnabled(kde_confdir);
1035  if (!enabled) {
1036  return;
1037  }
1038 
1039  HotPlugRulesList rules = getHotplugRules(kde_confdir);
1040  TQPtrList<SingleScreenData> screenInfoArray = readCurrentDisplayConfiguration();
1041 
1042  int i;
1043  int j;
1044  TQString bestRule;
1045  int bestRuleMatchCount = 0;
1046  SingleScreenData *screendata = NULL;
1047  HotPlugRulesList::Iterator it;
1048  for (it=rules.begin(); it != rules.end(); ++it) {
1049  // Compare each rule against the current display configuration
1050  // It an output matches the state given in the rule, increment matchCount
1051  HotPlugRule rule = *it;
1052  int matchCount = 0;
1053  int numberOfScreens = screenInfoArray.count();
1054  for (i=0;i<numberOfScreens;i++) {
1055  screendata = screenInfoArray.at(i);
1056  for (j=0; j<(*it).outputs.count(); j++) {
1057  if ((*it).outputs[j] != screendata->screenUniqueName) {
1058  continue;
1059  }
1060  if ((*it).states[j] == HotPlugRule::Connected) {
1061  if (screendata->screen_connected) {
1062  matchCount++;
1063  }
1064  }
1065  else if ((*it).states[j] == HotPlugRule::Disconnected) {
1066  if (!screendata->screen_connected) {
1067  matchCount++;
1068  }
1069  }
1070  }
1071  }
1072 
1073  if (matchCount > bestRuleMatchCount) {
1074  bestRuleMatchCount = matchCount;
1075  bestRule = rule.profileName;
1076  }
1077  }
1078 
1079  destroyScreenInformationObject(screenInfoArray);
1080 
1081  if (bestRuleMatchCount > 0) {
1082  // At least one rule matched...
1083  // Apply the profile name in bestRule to the display hardware
1084  applyDisplayConfiguration(bestRule, kde_confdir);
1085  }
1086 }
1087 
1088 void KRandrSimpleAPI::applyDisplayGamma(TQPtrList<SingleScreenData> screenInfoArray) {
1089  int i;
1090  Display *randr_display;
1091  XRROutputInfo *output_info;
1092  ScreenInfo *randr_screen_info;
1093  XRRCrtcGamma *gamma;
1094 
1095  SingleScreenData *screendata;
1096 
1097  if (isValid() == true) {
1098  randr_display = tqt_xdisplay();
1099  randr_screen_info = read_screen_info(randr_display);
1100  for (i = 0; i < screenInfoArray.count(); i++) {
1101  screendata = screenInfoArray.at(i);
1102  output_info = randr_screen_info->outputs[i]->info;
1103  CrtcInfo *current_crtc = randr_screen_info->outputs[i]->cur_crtc;
1104  if (!current_crtc) {
1105  continue;
1106  }
1107  // vvvvvvvvv This chunk of code is borrowed from xrandr vvvvvvvvvv
1108  int size = XRRGetCrtcGammaSize(randr_display, current_crtc->id);
1109  if (!size) {
1110  continue;
1111  }
1112  gamma = XRRAllocGamma(size);
1113  if (!gamma) {
1114  continue;
1115  }
1116  for (i = 0; i < size; i++) {
1117  if (screendata->gamma_red == 1.0)
1118  gamma->red[i] = i << 8;
1119  else
1120  gamma->red[i] = (pow((double)i/(double)(size-1), (double)screendata->gamma_red) * (double)(size-1)*256);
1121 
1122  if (screendata->gamma_green == 1.0)
1123  gamma->green[i] = i << 8;
1124  else
1125  gamma->green[i] = (pow((double)i/(double)(size-1), (double)screendata->gamma_green) * (double)(size-1)*256);
1126 
1127  if (screendata->gamma_blue == 1.0)
1128  gamma->blue[i] = i << 8;
1129  else
1130  gamma->blue[i] = (pow((double)i/(double)(size-1), (double)screendata->gamma_blue) * (double)(size-1)*256);
1131  }
1132  XRRSetCrtcGamma(randr_display, current_crtc->id, gamma);
1133  free(gamma);
1134  // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1135  }
1136  freeScreenInfoStructure(randr_screen_info);
1137  }
1138 }
1139 
1140 void KRandrSimpleAPI::applyDisplayDPMS(TQPtrList<SingleScreenData> screenInfoArray) {
1141  int i;
1142  Display *randr_display;
1143  XRROutputInfo *output_info;
1144  ScreenInfo *randr_screen_info;
1145  XRRCrtcGamma *gamma;
1146 
1147  SingleScreenData *screendata;
1148 
1149  if (isValid() == true) {
1150  randr_display = tqt_xdisplay();
1151  randr_screen_info = read_screen_info(randr_display);
1152  for (i = 0; i < screenInfoArray.count(); i++) {
1153  screendata = screenInfoArray.at(i);
1154  output_info = randr_screen_info->outputs[i]->info;
1155  CrtcInfo *current_crtc = randr_screen_info->outputs[i]->cur_crtc;
1156  if (!current_crtc) {
1157  continue;
1158  }
1159  if (!screendata->has_dpms) {
1160  continue;
1161  }
1162  if (screendata->enable_dpms) {
1163  DPMSSetTimeouts(randr_display, screendata->dpms_standby_delay, screendata->dpms_suspend_delay, screendata->dpms_off_delay);
1164  DPMSEnable(randr_display);
1165  }
1166  else {
1167  DPMSDisable(randr_display);
1168  }
1169  }
1170  freeScreenInfoStructure(randr_screen_info);
1171  }
1172 }
1173 
1174 void KRandrSimpleAPI::freeScreenInfoStructure(ScreenInfo* screen_info) {
1175  int i;
1176 
1177  for (i=0; i<screen_info->n_crtc; i++) {
1178  free(screen_info->crtcs[i]);
1179  }
1180  for (i=0; i<screen_info->n_output; i++) {
1181  free(screen_info->outputs[i]);
1182  }
1183  free(screen_info->outputs);
1184  free(screen_info->crtcs);
1185  free(screen_info);
1186 }
1187 
1188 TQPtrList<SingleScreenData> KRandrSimpleAPI::readCurrentDisplayConfiguration() {
1189  // Discover display information
1190  int i;
1191  int j;
1192 
1193  XRROutputInfo *output_info;
1194  SingleScreenData *screendata;
1195  TQPtrList<SingleScreenData> screenInfoArray;
1196 
1197  Display *randr_display;
1198  ScreenInfo *randr_screen_info;
1199 
1200  // Clear existing info
1201  destroyScreenInformationObject(screenInfoArray);
1202 
1203  int numberOfScreens = 0;
1204  if (isValid() == true) {
1205  randr_display = tqt_xdisplay();
1206  randr_screen_info = read_screen_info(randr_display);
1207  for (i = 0; i < randr_screen_info->n_output; i++) {
1208  output_info = randr_screen_info->outputs[i]->info;
1209  CrtcInfo *current_crtc = randr_screen_info->outputs[i]->cur_crtc;
1210 
1211  // Create new data object
1212  screendata = new SingleScreenData;
1213  screenInfoArray.append(screendata);
1214  screendata->screenUniqueName = TQString(i18n("%1:%2")).arg(":0").arg(capitalizeString(output_info->name)); // [FIXME] How can I get the name of the Xorg graphics driver currently in use?
1215  screendata->screenFriendlyName = TQString(i18n("%1. %2 output on %3")).arg(i+1).arg(capitalizeString(output_info->name)).arg(":0"); // [FIXME] How can I get the name of the Xorg graphics driver currently in use?
1216  screendata->generic_screen_detected = false;
1217 
1218  // Attempt to use KMS to find screen EDID and name
1219  TQString edid = getEDIDMonitorName(0, output_info->name); // [FIXME] Don't hardwire to card 0!
1220  if (!edid.isNull()) {
1221  screendata->screenFriendlyName = TQString(i18n("%1. %2 on %3 on card %4")).arg(i+1).arg(edid).arg(capitalizeString(output_info->name)).arg("0"); // [FIXME] How can I get the name of the Xorg graphics driver currently in use?
1222  }
1223 
1224  // Get resolutions
1225  bool screen_active;
1226  RandRScreen *cur_screen = 0;
1227  if (RR_Disconnected == randr_screen_info->outputs[i]->info->connection) {
1228  // Output DISCONNECTED
1229  screen_active = false;
1230  }
1231  else {
1232  if (randr_screen_info->outputs[i]->cur_crtc) {
1233  // Output CONNECTED and ON
1234  screen_active = true;
1235  cur_screen = new RandRScreen(i);
1236  }
1237  else {
1238  // Output CONNECTED and OFF
1239  screen_active = false;
1240  cur_screen = new RandRScreen(i);
1241  }
1242  }
1243 
1244  // Get DPMS information
1245  screendata->has_dpms = 1; // [FIXME] Master Xorg check for global DPMS support should go here if possible
1246  if (screendata->has_dpms) {
1247  CARD16 dpms_standby_delay;
1248  CARD16 dpms_suspend_delay;
1249  CARD16 dpms_off_delay;
1250  screendata->has_dpms = DPMSGetTimeouts(randr_display, &dpms_standby_delay, &dpms_suspend_delay, &dpms_off_delay);
1251  screendata->dpms_standby_delay = dpms_standby_delay;
1252  screendata->dpms_suspend_delay = dpms_suspend_delay;
1253  screendata->dpms_off_delay = dpms_off_delay;
1254  if (screendata->has_dpms) {
1255  CARD16 power_level;
1256  BOOL enable_dpms;
1257  screendata->has_dpms = DPMSInfo(randr_display, &power_level, &enable_dpms);
1258  screendata->enable_dpms = enable_dpms;
1259  }
1260  }
1261  if (!screendata->has_dpms) {
1262  screendata->enable_dpms = false;
1263  screendata->dpms_standby_delay = 0;
1264  screendata->dpms_suspend_delay = 0;
1265  screendata->dpms_off_delay = 0;
1266  }
1267 
1268  if (cur_screen) {
1269  screendata->screen_connected = true;
1270  for (int j = 0; j < cur_screen->numSizes(); j++) {
1271  screendata->resolutions.append(i18n("%1 x %2").arg(cur_screen->pixelSize(j).width()).arg(cur_screen->pixelSize(j).height()));
1272  }
1273  screendata->current_resolution_index = 0;
1274  if (current_crtc) {
1275  screendata->current_resolution_index = screendata->resolutions.findIndex(i18n("%1 x %2").arg(current_crtc->info->width).arg(current_crtc->info->height));
1276  }
1277  if (screendata->current_resolution_index < 0) {
1278  screendata->current_resolution_index = cur_screen->proposedSize();
1279  }
1280 
1281  // Get refresh rates
1282  TQStringList rr = cur_screen->refreshRates(screendata->current_resolution_index);
1283  for (TQStringList::Iterator it = rr.begin(); it != rr.end(); ++it) {
1284  screendata->refresh_rates.append(*it);
1285  }
1286  screendata->current_refresh_rate_index = cur_screen->proposedRefreshRate();
1287 
1288  // Get color depths
1289  // [FIXME]
1290  screendata->color_depths.append(i18n("Default"));
1291  screendata->current_color_depth_index = 0;
1292 
1293  // Get orientation flags
1294  // RandRScreen::Rotate0
1295  // RandRScreen::Rotate90
1296  // RandRScreen::Rotate180
1297  // RandRScreen::Rotate270
1298  // RandRScreen::ReflectX
1299  // RandRScreen::ReflectY
1300 
1301  screendata->rotations.append(i18n(ROTATION_0_DEGREES_STRING));
1302  screendata->rotations.append(i18n(ROTATION_90_DEGREES_STRING));
1303  screendata->rotations.append(i18n(ROTATION_180_DEGREES_STRING));
1304  screendata->rotations.append(i18n(ROTATION_270_DEGREES_STRING));
1305  screendata->supports_transformations = (cur_screen->rotations() != RandRScreen::Rotate0);
1306  if (screendata->supports_transformations) {
1307  screendata->current_orientation_mask = cur_screen->proposedRotation();
1308  switch (screendata->current_orientation_mask & RandRScreen::RotateMask) {
1309  case RandRScreen::Rotate0:
1310  screendata->current_rotation_index = 0;
1311  break;
1312  case RandRScreen::Rotate90:
1313  screendata->current_rotation_index = 1;
1314  break;
1315  case RandRScreen::Rotate180:
1316  screendata->current_rotation_index = 2;
1317  break;
1318  case RandRScreen::Rotate270:
1319  screendata->current_rotation_index = 3;
1320  break;
1321  default:
1322  // Shouldn't hit this one
1323  Q_ASSERT(screendata->current_orientation_mask & RandRScreen::RotateMask);
1324  screendata->current_rotation_index = 0;
1325  break;
1326  }
1327  screendata->has_x_flip = (screendata->current_orientation_mask & RandRScreen::ReflectX);
1328  screendata->has_y_flip = (screendata->current_orientation_mask & RandRScreen::ReflectY);
1329  }
1330  else {
1331  screendata->has_x_flip = false;
1332  screendata->has_y_flip = false;
1333  screendata->current_rotation_index = 0;
1334  }
1335 
1336  // Determine if this display is primary and/or extended
1337  RROutput primaryoutput = XRRGetOutputPrimary(tqt_xdisplay(), DefaultRootWindow(tqt_xdisplay()));
1338  if (primaryoutput == randr_screen_info->outputs[i]->id) {
1339  screendata->is_primary = false;
1340  }
1341  else {
1342  screendata->is_primary = true;
1343  }
1344  screendata->is_extended = screen_active;
1345  if (!screendata->is_extended) {
1346  screendata->is_primary = false;
1347  }
1348 
1349  // Get this screen's absolute position
1350  screendata->absolute_x_position = 0;
1351  screendata->absolute_y_position = 0;
1352  if (current_crtc) {
1353  screendata->absolute_x_position = current_crtc->info->x;
1354  screendata->absolute_y_position = current_crtc->info->y;
1355  }
1356 
1357  // Get this screen's current resolution
1358  screendata->current_x_pixel_count = cur_screen->pixelSize(screendata->current_resolution_index).width();
1359  screendata->current_y_pixel_count = cur_screen->pixelSize(screendata->current_resolution_index).height();
1360 
1361  // Get this screen's current gamma values
1362  // [FIXME]
1363  // This attempts to guess a gamma value based on the LUT settings at 50%
1364  // It may not always be 100% correct, or even anywhere close...
1365  // Essentially it "undoes" the LUT gamma calculation from xrandr
1366  // lut_gamma->green[i] = (pow(i/(size - 1), desired_gamma.green) * (size - 1) * 256);
1367  if (current_crtc) {
1368  //int slot = 127;
1369  int slot = 7;
1370  int size = XRRGetCrtcGammaSize(randr_display, current_crtc->id);
1371  XRRCrtcGamma *gammastruct = XRRGetCrtcGamma (randr_display, current_crtc->id);
1372  screendata->gamma_red = log(gammastruct->red[slot]/((size-1.0)*256.0))/log(slot/(size-1.0));
1373  screendata->gamma_green = log(gammastruct->green[slot]/((size-1.0)*256.0))/log(slot/(size-1.0));
1374  screendata->gamma_blue = log(gammastruct->blue[slot]/((size-1.0)*256.0))/log(slot/(size-1.0));
1375  }
1376  else {
1377  screendata->gamma_red = 2.2;
1378  screendata->gamma_green = 2.2;
1379  screendata->gamma_blue = 2.2;
1380  }
1381  // Round off the gamma to one decimal place
1382  screendata->gamma_red = floorf(screendata->gamma_red * 10 + 0.5) / 10;
1383  screendata->gamma_green = floorf(screendata->gamma_green * 10 + 0.5) / 10;
1384  screendata->gamma_blue = floorf(screendata->gamma_blue * 10 + 0.5) / 10;
1385 
1386  delete cur_screen;
1387  }
1388  else {
1389  // Fill in generic data for this disconnected output
1390  screendata->screenFriendlyName = screendata->screenFriendlyName + TQString(" (") + i18n("disconnected") + TQString(")");
1391  screendata->screen_connected = false;
1392 
1393  screendata->resolutions = i18n("Default");
1394  screendata->refresh_rates = i18n("Default");
1395  screendata->color_depths = i18n("Default");
1396  screendata->rotations = i18n("N/A");
1397 
1398  screendata->current_resolution_index = 0;
1399  screendata->current_refresh_rate_index = 0;
1400  screendata->current_color_depth_index = 0;
1401 
1402  screendata->gamma_red = 2.2;
1403  screendata->gamma_green = 2.2;
1404  screendata->gamma_blue = 2.2;
1405 
1406  screendata->current_rotation_index = 0;
1407  screendata->current_orientation_mask = 0;
1408  screendata->has_x_flip = false;
1409  screendata->has_y_flip = false;
1410  screendata->supports_transformations = false;
1411 
1412  screendata->is_primary = false;
1413  screendata->is_extended = false;
1414  screendata->absolute_x_position = 0;
1415  screendata->absolute_y_position = 0;
1416  screendata->current_x_pixel_count = 640;
1417  screendata->current_y_pixel_count = 480;
1418  }
1419 
1420  // Check for more screens...
1421  numberOfScreens++;
1422  }
1423 
1424  freeScreenInfoStructure(randr_screen_info);
1425  }
1426  else {
1427  screendata = new SingleScreenData;
1428  screenInfoArray.append(screendata);
1429 
1430  // Fill in a bunch of generic data
1431  screendata->screenFriendlyName = i18n("Default output on generic video card");
1432  screendata->generic_screen_detected = true;
1433  screendata->screen_connected = true;
1434 
1435  screendata->resolutions = i18n("Default");
1436  screendata->refresh_rates = i18n("Default");
1437  screendata->color_depths = i18n("Default");
1438  screendata->rotations = i18n("N/A");
1439 
1440  screendata->current_resolution_index = 0;
1441  screendata->current_refresh_rate_index = 0;
1442  screendata->current_color_depth_index = 0;
1443 
1444  screendata->gamma_red = 2.2;
1445  screendata->gamma_green = 2.2;
1446  screendata->gamma_blue = 2.2;
1447 
1448  screendata->current_rotation_index = 0;
1449  screendata->current_orientation_mask = 0;
1450  screendata->has_x_flip = false;
1451  screendata->has_y_flip = false;
1452  screendata->supports_transformations = false;
1453 
1454  screendata->is_primary = true;
1455  screendata->is_extended = true;
1456  screendata->absolute_x_position = 0;
1457  screendata->absolute_y_position = 0;
1458  screendata->current_x_pixel_count = 640;
1459  screendata->current_y_pixel_count = 480;
1460 
1461  numberOfScreens++;
1462  }
1463 
1464  bool primary_set = false;
1465  for ( screendata=screenInfoArray.first(); screendata; screendata=screenInfoArray.next() ) {
1466  if (screendata->is_primary) {
1467  primary_set = true;
1468  break;
1469  }
1470  }
1471  // If there is no primary monitor set, xrandr is probably not functioning correctly!
1472  Q_ASSERT(primary_set);
1473  if (!primary_set) {
1474  // [FIXME]
1475  // Set this on the real primary monitor only!
1476  screendata = screenInfoArray.at(0);
1477  screendata->is_primary = true;
1478  }
1479 
1480  return screenInfoArray;
1481 }
1482 
1483 TQString KRandrSimpleAPI::clearIccConfiguration() {
1484  // Clear ICC settings with XCalib
1485  TQString icc_command;
1486  FILE *pipe_xcalib;
1487  char xcalib_result[2048];
1488  int i;
1489  xcalib_result[0]=0;
1490 
1491  icc_command = TQString("xcalib -c");
1492  if ((pipe_xcalib = popen(icc_command.ascii(), "r")) == NULL)
1493  {
1494  printf("Xcalib pipe error [xcalib clear]\n");
1495  }
1496  else {
1497  if (fgets(xcalib_result, 2048, pipe_xcalib)) {
1498  pclose(pipe_xcalib);
1499  for (i=1;i<2048;i++) {
1500  if (xcalib_result[i] == 0) {
1501  xcalib_result[i-1]=0;
1502  i=2048;
1503  }
1504  }
1505  if (strlen(xcalib_result) > 2) {
1506  return xcalib_result;
1507  }
1508  }
1509  else {
1510  return "";
1511  }
1512  }
1513  return "";
1514 }
1515 
1516 ScreenInfo* KRandrSimpleAPI::read_screen_info (Display *display)
1517 {
1518  return internal_read_screen_info(display);
1519 }
1520 
1521 int KRandrSimpleAPI::set_screen_size (ScreenInfo *screen_info)
1522 {
1523  return internal_set_screen_size(screen_info);
1524 }
1525 
1526 void KRandrSimpleAPI::output_auto (ScreenInfo *screen_info, OutputInfo *output_info)
1527 {
1528  internal_output_auto (screen_info, output_info);
1529 }
1530 
1531 void KRandrSimpleAPI::output_off(ScreenInfo *screen_info, OutputInfo *output)
1532 {
1533  internal_output_off(screen_info, output);
1534 }
1535 
1536 CrtcInfo* KRandrSimpleAPI::auto_find_crtc (ScreenInfo *screen_info, OutputInfo *output_info)
1537 {
1538  return internal_auto_find_crtc (screen_info, output_info);
1539 }
1540 
1541 XRRModeInfo *KRandrSimpleAPI::find_mode_by_xid (ScreenInfo *screen_info, RRMode mode_id)
1542 {
1543  return internal_find_mode_by_xid (screen_info, mode_id);
1544 }
1545 
1546 int KRandrSimpleAPI::mode_height (XRRModeInfo *mode_info, Rotation rotation)
1547 {
1548  return internal_mode_height (mode_info, rotation);
1549 }
1550 
1551 int KRandrSimpleAPI::mode_width (XRRModeInfo *mode_info, Rotation rotation)
1552 {
1553  return internal_mode_width (mode_info, rotation);
1554 }
1555 
1556 int KRandrSimpleAPI::get_width_by_output_id (ScreenInfo *screen_info, RROutput output_id)
1557 {
1558  return internal_get_width_by_output_id (screen_info, output_id);
1559 }
1560 
1561 int KRandrSimpleAPI::get_height_by_output_id (ScreenInfo *screen_info, RROutput output_id)
1562 {
1563  return internal_get_height_by_output_id (screen_info, output_id);
1564 }
1565 
1566 char *KRandrSimpleAPI::get_output_name (ScreenInfo *screen_info, RROutput id)
1567 {
1568  return internal_get_output_name (screen_info, id);
1569 }
1570 
1571 Status KRandrSimpleAPI::crtc_apply (CrtcInfo *crtc_info)
1572 {
1573  return internal_crtc_apply (crtc_info);
1574 }
1575 
1576 Status KRandrSimpleAPI::crtc_disable (CrtcInfo *crtc)
1577 {
1578  return internal_crtc_disable (crtc);
1579 }
1580 
1581 int KRandrSimpleAPI::main_low_apply (ScreenInfo *screen_info)
1582 {
1583  return internal_main_low_apply (screen_info);
1584 }
1585 
1586 void KRandrSimpleAPI::set_primary_output (ScreenInfo *screen_info, RROutput output_id)
1587 {
1588  internal_output_set_primary(screen_info, output_id);
1589 }
1590 
1591 bool KRandrSimpleAPI::kRandrHasRandr(void)
1592 {
1593  return isValid();
1594 }
1595 
1596 const char *KRandrSimpleAPI::kRandrVersion(void)
1597 {
1598  return "0.9.5";
1599 }
1600 
1601 const char *KRandrSimpleAPI::kRandrCopyright(void)
1602 {
1603  return "LibKRandr 0.9.5 (C)2010 Timothy Pearson <kb9vqf@pearsoncomputing.net>. U.S.A.";
1604 }
1605 
1606 /* * * * * *
1607 
1608  Under this line (------) there's only a C wrapper for the KRandrSimpleAPI class
1609 
1610 * * * * * */
1611 const char *kRandrVersion(void)
1612 {
1613  return KRandrSimpleAPI::kRandrVersion();
1614 }
1615 
1616 const char *kRandrCopyright(void)
1617 {
1618  return KRandrSimpleAPI::kRandrCopyright();
1619 }
1620 

tderandr

Skip menu "tderandr"
  • Main Page
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

tderandr

Skip menu "tderandr"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • knewstuff
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tderandr by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.