ssetbg.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* SLiM - Simple Login Manager
  2. Copyright (C) 1997, 1998 Per Liden
  3. Copyright (C) 2004-06 Simone Rota <sip@varlock.com>
  4. Copyright (C) 2004-06 Johannes Winkelmann <jw@tks6.net>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. */
  10. /* Simple app to set the backround image of the X root window */
  11. #include <iostream>
  12. #include "image.h"
  13. #define DISPLAY ":0.0"
  14. #define BG_STRETCH 1
  15. #define BG_TILE 2
  16. #define BG_CENTER 3
  17. #define BG_COLOR 4
  18. using namespace std;
  19. void setBackground(int bgstyle, char* fname, char* bgcolor) {
  20. bool loaded;
  21. Display* Dpy;
  22. int Scr;
  23. Window Root;
  24. string filename;
  25. string bg_color;
  26. if (fname != NULL) {
  27. filename.assign(fname);
  28. }
  29. if (bgcolor != NULL) {
  30. bg_color.assign(bgcolor);
  31. }
  32. if((Dpy = XOpenDisplay(DISPLAY)) == 0) {
  33. cerr << "could not open display" << endl;
  34. exit(1);
  35. }
  36. Scr = DefaultScreen(Dpy);
  37. Root = RootWindow(Dpy, Scr);
  38. Image* image = new Image;
  39. if (bgstyle != BG_COLOR) {
  40. loaded = image->Read(fname);
  41. if (!loaded) {
  42. cerr << "Could not load image!" << endl;
  43. exit(1);
  44. }
  45. }
  46. if (bgstyle == BG_STRETCH) {
  47. image->Resize(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)));
  48. } else if (bgstyle == BG_TILE) {
  49. image->Tile(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)));
  50. } else if (bgstyle == BG_CENTER) {
  51. image->Center(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)),
  52. bg_color.c_str());
  53. } else {
  54. image->Center(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)),
  55. bg_color.c_str());
  56. }
  57. Pixmap p = image->createPixmap(Dpy, Scr, Root);
  58. XSetWindowBackgroundPixmap(Dpy, Root, p);
  59. XClearWindow(Dpy, Root);
  60. XFlush(Dpy);
  61. delete image;
  62. XCloseDisplay(Dpy);
  63. }
  64. int main(int argc, char** argv) {
  65. int tmp;
  66. char* filename;
  67. char* bg_color;
  68. int bg_style;
  69. bg_style = -1;
  70. while((tmp = getopt(argc, argv, "hs:t:n:c:?")) != EOF) {
  71. switch (tmp) {
  72. case 's':
  73. filename = optarg;
  74. bg_style = BG_STRETCH;
  75. break;
  76. case 't':
  77. filename = optarg;
  78. bg_style = BG_TILE;
  79. break;
  80. case 'n':
  81. filename = optarg;
  82. bg_style = BG_CENTER;
  83. break;
  84. case 'c':
  85. bg_color = optarg;
  86. bg_style = BG_COLOR;
  87. break;
  88. case '?':
  89. cerr << endl;
  90. case 'h':
  91. cerr << "usage: ssetbg [-c color] [-s|-t|-n filename]" << endl
  92. << "Available modes:" << endl
  93. << " -s: stretch" << endl
  94. << " -t: tiled" << endl
  95. << " -n: center" << endl
  96. << " -c: color" << endl;
  97. exit(0);
  98. break;
  99. }
  100. }
  101. if (bg_style == -1) {
  102. if (argc > 0)
  103. filename = argv[1];
  104. bg_style = BG_STRETCH;
  105. }
  106. if (bg_style != BG_COLOR && filename == NULL) {
  107. cerr << "No filename given!" << endl;
  108. exit(1);
  109. }
  110. if (bg_style == BG_COLOR && bg_color == NULL) {
  111. cerr << "No color given!" << endl;
  112. exit(1);
  113. }
  114. // if (bg_color == NULL)
  115. setBackground(bg_style, filename, bg_color);
  116. return(0);
  117. }