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

938 lines
26 KiB

  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. #include <sstream>
  11. #include <poll.h>
  12. #include <X11/extensions/Xrandr.h>
  13. #include "panel.h"
  14. using namespace std;
  15. Panel::Panel(Display* dpy, int scr, Window root, Cfg* config,
  16. const string& themedir, PanelType panel_mode) {
  17. /* Set display */
  18. Dpy = dpy;
  19. Scr = scr;
  20. Root = root;
  21. cfg = config;
  22. mode = panel_mode;
  23. session_name = "";
  24. session_exec = "";
  25. if (mode == Mode_Lock) {
  26. Win = root;
  27. viewport = GetPrimaryViewport();
  28. }
  29. /* Init GC */
  30. XGCValues gcv;
  31. unsigned long gcm;
  32. gcm = GCForeground|GCBackground|GCGraphicsExposures;
  33. gcv.foreground = GetColor("black");
  34. gcv.background = GetColor("white");
  35. gcv.graphics_exposures = False;
  36. if (mode == Mode_Lock)
  37. TextGC = XCreateGC(Dpy, Win, gcm, &gcv);
  38. else
  39. TextGC = XCreateGC(Dpy, Root, gcm, &gcv);
  40. if (mode == Mode_Lock) {
  41. gcm = GCGraphicsExposures;
  42. gcv.graphics_exposures = False;
  43. WinGC = XCreateGC(Dpy, Win, gcm, &gcv);
  44. if (WinGC < 0) {
  45. cerr << APPNAME
  46. << ": failed to create pixmap\n.";
  47. exit(ERR_EXIT);
  48. }
  49. }
  50. font = XftFontOpenName(Dpy, Scr, cfg->getOption("input_font").c_str());
  51. welcomefont = XftFontOpenName(Dpy, Scr, cfg->getOption("welcome_font").c_str());
  52. introfont = XftFontOpenName(Dpy, Scr, cfg->getOption("intro_font").c_str());
  53. enterfont = XftFontOpenName(Dpy, Scr, cfg->getOption("username_font").c_str());
  54. msgfont = XftFontOpenName(Dpy, Scr, cfg->getOption("msg_font").c_str());
  55. Visual* visual = DefaultVisual(Dpy, Scr);
  56. Colormap colormap = DefaultColormap(Dpy, Scr);
  57. /* NOTE: using XftColorAllocValue() would be a better solution. Lazy me. */
  58. XftColorAllocName(Dpy, visual, colormap, cfg->getOption("input_color").c_str(), &inputcolor);
  59. XftColorAllocName(Dpy, visual, colormap, cfg->getOption("input_shadow_color").c_str(), &inputshadowcolor);
  60. XftColorAllocName(Dpy, visual, colormap, cfg->getOption("welcome_color").c_str(), &welcomecolor);
  61. XftColorAllocName(Dpy, visual, colormap, cfg->getOption("welcome_shadow_color").c_str(), &welcomeshadowcolor);
  62. XftColorAllocName(Dpy, visual, colormap, cfg->getOption("username_color").c_str(), &entercolor);
  63. XftColorAllocName(Dpy, visual, colormap, cfg->getOption("username_shadow_color").c_str(), &entershadowcolor);
  64. XftColorAllocName(Dpy, visual, colormap, cfg->getOption("msg_color").c_str(), &msgcolor);
  65. XftColorAllocName(Dpy, visual, colormap, cfg->getOption("msg_shadow_color").c_str(), &msgshadowcolor);
  66. XftColorAllocName(Dpy, visual, colormap, cfg->getOption("intro_color").c_str(), &introcolor);
  67. XftColorAllocName(Dpy, visual, colormap,
  68. cfg->getOption("session_color").c_str(), &sessioncolor);
  69. XftColorAllocName(Dpy, visual, colormap,
  70. cfg->getOption("session_shadow_color").c_str(), &sessionshadowcolor);
  71. /* Load properties from config / theme */
  72. input_name_x = cfg->getIntOption("input_name_x");
  73. input_name_y = cfg->getIntOption("input_name_y");
  74. input_pass_x = cfg->getIntOption("input_pass_x");
  75. input_pass_y = cfg->getIntOption("input_pass_y");
  76. inputShadowXOffset = cfg->getIntOption("input_shadow_xoffset");
  77. inputShadowYOffset = cfg->getIntOption("input_shadow_yoffset");
  78. if (input_pass_x < 0 || input_pass_y < 0){ /* single inputbox mode */
  79. input_pass_x = input_name_x;
  80. input_pass_y = input_name_y;
  81. }
  82. /* Load panel and background image */
  83. string panelpng = "";
  84. panelpng = panelpng + themedir +"/panel.png";
  85. image = new Image;
  86. bool loaded = image->Read(panelpng.c_str());
  87. if (!loaded) { /* try jpeg if png failed */
  88. panelpng = themedir + "/panel.jpg";
  89. loaded = image->Read(panelpng.c_str());
  90. if (!loaded) {
  91. logStream << APPNAME
  92. << ": could not load panel image for theme '"
  93. << basename((char*)themedir.c_str()) << "'"
  94. << endl;
  95. exit(ERR_EXIT);
  96. }
  97. }
  98. Image* bg = new Image();
  99. string bgstyle = cfg->getOption("background_style");
  100. if (bgstyle != "color") {
  101. panelpng = themedir +"/background.png";
  102. loaded = bg->Read(panelpng.c_str());
  103. if (!loaded) { /* try jpeg if png failed */
  104. panelpng = themedir + "/background.jpg";
  105. loaded = bg->Read(panelpng.c_str());
  106. if (!loaded){
  107. logStream << APPNAME
  108. << ": could not load background image for theme '"
  109. << basename((char*)themedir.c_str()) << "'"
  110. << endl;
  111. exit(ERR_EXIT);
  112. }
  113. }
  114. }
  115. if (mode == Mode_Lock) {
  116. if (bgstyle == "stretch")
  117. bg->Resize(viewport.width, viewport.height);
  118. //bg->Resize(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)),
  119. // XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)));
  120. else if (bgstyle == "tile")
  121. bg->Tile(viewport.width, viewport.height);
  122. else if (bgstyle == "center") {
  123. string hexvalue = cfg->getOption("background_color");
  124. hexvalue = hexvalue.substr(1,6);
  125. bg->Center(viewport.width,
  126. viewport.height,
  127. hexvalue.c_str());
  128. } else { // plain color or error
  129. string hexvalue = cfg->getOption("background_color");
  130. hexvalue = hexvalue.substr(1,6);
  131. bg->Center(viewport.width,
  132. viewport.height,
  133. hexvalue.c_str());
  134. }
  135. } else {
  136. if (bgstyle == "stretch") {
  137. bg->Resize(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)),
  138. XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)));
  139. } else if (bgstyle == "tile") {
  140. bg->Tile(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)),
  141. XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)));
  142. } else if (bgstyle == "center") {
  143. string hexvalue = cfg->getOption("background_color");
  144. hexvalue = hexvalue.substr(1,6);
  145. bg->Center(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)),
  146. XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)),
  147. hexvalue.c_str());
  148. } else { /* plain color or error */
  149. string hexvalue = cfg->getOption("background_color");
  150. hexvalue = hexvalue.substr(1,6);
  151. bg->Center(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)),
  152. XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)),
  153. hexvalue.c_str());
  154. }
  155. }
  156. string cfgX = cfg->getOption("input_panel_x");
  157. string cfgY = cfg->getOption("input_panel_y");
  158. if (mode == Mode_Lock) {
  159. X = Cfg::absolutepos(cfgX, viewport.width, image->Width());
  160. Y = Cfg::absolutepos(cfgY, viewport.height, image->Height());
  161. input_name_x += X;
  162. input_name_y += Y;
  163. input_pass_x += X;
  164. input_pass_y += Y;
  165. } else {
  166. X = Cfg::absolutepos(cfgX, XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), image->Width());
  167. Y = Cfg::absolutepos(cfgY, XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)), image->Height());
  168. }
  169. if (mode == Mode_Lock) {
  170. /* Merge image into background without crop */
  171. image->Merge_non_crop(bg, X, Y);
  172. PanelPixmap = image->createPixmap(Dpy, Scr, Win);
  173. } else {
  174. /* Merge image into background */
  175. image->Merge(bg, X, Y);
  176. PanelPixmap = image->createPixmap(Dpy, Scr, Root);
  177. }
  178. delete bg;
  179. /* Read (and substitute vars in) the welcome message */
  180. welcome_message = cfg->getWelcomeMessage();
  181. intro_message = cfg->getOption("intro_msg");
  182. if (mode == Mode_Lock) {
  183. SetName(getenv("USER"));
  184. field = Get_Passwd;
  185. OnExpose();
  186. }
  187. }
  188. Panel::~Panel() {
  189. Visual* visual = DefaultVisual(Dpy, Scr);
  190. Colormap colormap = DefaultColormap(Dpy, Scr);
  191. XftColorFree(Dpy, visual, colormap, &inputcolor);
  192. XftColorFree(Dpy, visual, colormap, &inputshadowcolor);
  193. XftColorFree(Dpy, visual, colormap, &welcomecolor);
  194. XftColorFree(Dpy, visual, colormap, &welcomeshadowcolor);
  195. XftColorFree(Dpy, visual, colormap, &entercolor);
  196. XftColorFree(Dpy, visual, colormap, &entershadowcolor);
  197. XftColorFree(Dpy, visual, colormap, &msgcolor);
  198. XftColorFree(Dpy, visual, colormap, &msgshadowcolor);
  199. XftColorFree(Dpy, visual, colormap, &introcolor);
  200. XftColorFree(Dpy, visual, colormap, &sessioncolor);
  201. XftColorFree(Dpy, visual, colormap, &sessionshadowcolor);
  202. XFreeGC(Dpy, TextGC);
  203. XftFontClose(Dpy, font);
  204. XftFontClose(Dpy, msgfont);
  205. XftFontClose(Dpy, introfont);
  206. XftFontClose(Dpy, welcomefont);
  207. XftFontClose(Dpy, enterfont);
  208. if (mode == Mode_Lock)
  209. XFreeGC(Dpy, WinGC);
  210. delete image;
  211. }
  212. void Panel::OpenPanel() {
  213. /* Create window */
  214. Win = XCreateSimpleWindow(Dpy, Root, X, Y,
  215. image->Width(),
  216. image->Height(),
  217. 0, GetColor("white"), GetColor("white"));
  218. /* Events */
  219. XSelectInput(Dpy, Win, ExposureMask | KeyPressMask);
  220. /* Set background */
  221. XSetWindowBackgroundPixmap(Dpy, Win, PanelPixmap);
  222. /* Show window */
  223. XMapWindow(Dpy, Win);
  224. XMoveWindow(Dpy, Win, X, Y); /* override wm positioning (for tests) */
  225. /* Grab keyboard */
  226. XGrabKeyboard(Dpy, Win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
  227. XFlush(Dpy);
  228. }
  229. void Panel::ClosePanel() {
  230. XUngrabKeyboard(Dpy, CurrentTime);
  231. XUnmapWindow(Dpy, Win);
  232. XDestroyWindow(Dpy, Win);
  233. XFlush(Dpy);
  234. }
  235. void Panel::ClearPanel() {
  236. session_name = "";
  237. session_exec = "";
  238. Reset();
  239. XClearWindow(Dpy, Root);
  240. XClearWindow(Dpy, Win);
  241. Cursor(SHOW);
  242. ShowText();
  243. XFlush(Dpy);
  244. }
  245. void Panel::WrongPassword(int timeout) {
  246. string message;
  247. XGlyphInfo extents;
  248. #if 0
  249. if (CapsLockOn)
  250. message = cfg->getOption("passwd_feedback_capslock");
  251. else
  252. #endif
  253. message = cfg->getOption("passwd_feedback_msg");
  254. XftDraw *draw = XftDrawCreate(Dpy, Win,
  255. DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr));
  256. XftTextExtents8(Dpy, msgfont, reinterpret_cast<const XftChar8*>(message.c_str()),
  257. message.length(), &extents);
  258. string cfgX = cfg->getOption("passwd_feedback_x");
  259. string cfgY = cfg->getOption("passwd_feedback_y");
  260. int shadowXOffset = cfg->getIntOption("msg_shadow_xoffset");
  261. int shadowYOffset = cfg->getIntOption("msg_shadow_yoffset");
  262. int msg_x = Cfg::absolutepos(cfgX, XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), extents.width);
  263. int msg_y = Cfg::absolutepos(cfgY, XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)), extents.height);
  264. OnExpose();
  265. SlimDrawString8(draw, &msgcolor, msgfont, msg_x, msg_y, message,
  266. &msgshadowcolor, shadowXOffset, shadowYOffset);
  267. if (cfg->getOption("bell") == "1")
  268. XBell(Dpy, 100);
  269. XFlush(Dpy);
  270. sleep(timeout);
  271. ResetPasswd();
  272. OnExpose();
  273. // The message should stay on the screen even after the password field is
  274. // cleared, methinks. I don't like this solution, but it works.
  275. SlimDrawString8(draw, &msgcolor, msgfont, msg_x, msg_y, message,
  276. &msgshadowcolor, shadowXOffset, shadowYOffset);
  277. XSync(Dpy, True);
  278. XftDrawDestroy(draw);
  279. }
  280. void Panel::Message(const string& text) {
  281. string cfgX, cfgY;
  282. XGlyphInfo extents;
  283. XftDraw *draw;
  284. if (mode == Mode_Lock)
  285. draw = XftDrawCreate(Dpy, Win,
  286. DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr));
  287. else
  288. draw = XftDrawCreate(Dpy, Root,
  289. DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr));
  290. XftTextExtents8(Dpy, msgfont,
  291. reinterpret_cast<const XftChar8*>(text.c_str()),
  292. text.length(), &extents);
  293. cfgX = cfg->getOption("msg_x");
  294. cfgY = cfg->getOption("msg_y");
  295. int shadowXOffset = cfg->getIntOption("msg_shadow_xoffset");
  296. int shadowYOffset = cfg->getIntOption("msg_shadow_yoffset");
  297. int msg_x, msg_y;
  298. if (mode == Mode_Lock) {
  299. msg_x = Cfg::absolutepos(cfgX, viewport.width, extents.width);
  300. msg_y = Cfg::absolutepos(cfgY, viewport.height, extents.height);
  301. } else {
  302. msg_x = Cfg::absolutepos(cfgX, XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), extents.width);
  303. msg_y = Cfg::absolutepos(cfgY, XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)), extents.height);
  304. }
  305. SlimDrawString8 (draw, &msgcolor, msgfont, msg_x, msg_y,
  306. text,
  307. &msgshadowcolor,
  308. shadowXOffset, shadowYOffset);
  309. XFlush(Dpy);
  310. XftDrawDestroy(draw);
  311. }
  312. void Panel::Error(const string& text) {
  313. ClosePanel();
  314. Message(text);
  315. sleep(ERROR_DURATION);
  316. OpenPanel();
  317. ClearPanel();
  318. }
  319. unsigned long Panel::GetColor(const char* colorname) {
  320. XColor color;
  321. XWindowAttributes attributes;
  322. if (mode == Mode_Lock)
  323. XGetWindowAttributes(Dpy, Win, &attributes);
  324. else
  325. XGetWindowAttributes(Dpy, Root, &attributes);
  326. color.pixel = 0;
  327. if(!XParseColor(Dpy, attributes.colormap, colorname, &color))
  328. logStream << APPNAME << ": can't parse color " << colorname << endl;
  329. else if(!XAllocColor(Dpy, attributes.colormap, &color))
  330. logStream << APPNAME << ": can't allocate color " << colorname << endl;
  331. return color.pixel;
  332. }
  333. void Panel::Cursor(int visible) {
  334. const char* text = NULL;
  335. int xx = 0, yy = 0, y2 = 0, cheight = 0;
  336. const char* txth = "Wj"; /* used to get cursor height */
  337. if (mode == Mode_Lock) {
  338. text = HiddenPasswdBuffer.c_str();
  339. xx = input_pass_x;
  340. yy = input_pass_y;
  341. } else {
  342. switch(field) {
  343. case Get_Passwd:
  344. text = HiddenPasswdBuffer.c_str();
  345. xx = input_pass_x;
  346. yy = input_pass_y;
  347. break;
  348. case Get_Name:
  349. text = NameBuffer.c_str();
  350. xx = input_name_x;
  351. yy = input_name_y;
  352. break;
  353. }
  354. }
  355. XGlyphInfo extents;
  356. XftTextExtents8(Dpy, font, (XftChar8*)txth, strlen(txth), &extents);
  357. cheight = extents.height;
  358. y2 = yy - extents.y + extents.height;
  359. XftTextExtents8(Dpy, font, (XftChar8*)text, strlen(text), &extents);
  360. xx += extents.width;
  361. if(visible == SHOW) {
  362. if (mode == Mode_Lock) {
  363. xx += viewport.x;
  364. yy += viewport.y;
  365. y2 += viewport.y;
  366. }
  367. XSetForeground(Dpy, TextGC,
  368. GetColor(cfg->getOption("input_color").c_str()));
  369. XDrawLine(Dpy, Win, TextGC,
  370. xx+1, yy-cheight,
  371. xx+1, y2);
  372. } else {
  373. if (mode == Mode_Lock)
  374. ApplyBackground(Rectangle(xx+1, yy-cheight,
  375. 1, y2-(yy-cheight)+1));
  376. else
  377. XClearArea(Dpy, Win, xx+1, yy-cheight,
  378. 1, y2-(yy-cheight)+1, false);
  379. }
  380. }
  381. void Panel::EventHandler(const Panel::FieldType& curfield) {
  382. XEvent event;
  383. field = curfield;
  384. bool loop = true;
  385. if (mode == Mode_DM)
  386. OnExpose();
  387. struct pollfd x11_pfd = {0};
  388. x11_pfd.fd = ConnectionNumber(Dpy);
  389. x11_pfd.events = POLLIN;
  390. while (loop) {
  391. if (XPending(Dpy) || poll(&x11_pfd, 1, -1) > 0) {
  392. while(XPending(Dpy)) {
  393. XNextEvent(Dpy, &event);
  394. switch(event.type) {
  395. case Expose:
  396. OnExpose();
  397. break;
  398. case KeyPress:
  399. loop=OnKeyPress(event);
  400. break;
  401. }
  402. }
  403. }
  404. }
  405. return;
  406. }
  407. void Panel::OnExpose(void) {
  408. XftDraw *draw = XftDrawCreate(Dpy, Win,
  409. DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr));
  410. if (mode == Mode_Lock)
  411. ApplyBackground();
  412. else
  413. XClearWindow(Dpy, Win);
  414. if (input_pass_x != input_name_x || input_pass_y != input_name_y){
  415. SlimDrawString8 (draw, &inputcolor, font, input_name_x, input_name_y,
  416. NameBuffer,
  417. &inputshadowcolor,
  418. inputShadowXOffset, inputShadowYOffset);
  419. SlimDrawString8 (draw, &inputcolor, font, input_pass_x, input_pass_y,
  420. HiddenPasswdBuffer,
  421. &inputshadowcolor,
  422. inputShadowXOffset, inputShadowYOffset);
  423. } else { /*single input mode */
  424. switch(field) {
  425. case Get_Passwd:
  426. SlimDrawString8 (draw, &inputcolor, font,
  427. input_pass_x, input_pass_y,
  428. HiddenPasswdBuffer,
  429. &inputshadowcolor,
  430. inputShadowXOffset, inputShadowYOffset);
  431. break;
  432. case Get_Name:
  433. SlimDrawString8 (draw, &inputcolor, font,
  434. input_name_x, input_name_y,
  435. NameBuffer,
  436. &inputshadowcolor,
  437. inputShadowXOffset, inputShadowYOffset);
  438. break;
  439. }
  440. }
  441. XftDrawDestroy (draw);
  442. Cursor(SHOW);
  443. ShowText();
  444. }
  445. void Panel::EraseLastChar(string &formerString) {
  446. switch(field) {
  447. case GET_NAME:
  448. if (! NameBuffer.empty()) {
  449. formerString=NameBuffer;
  450. NameBuffer.erase(--NameBuffer.end());
  451. }
  452. break;
  453. case GET_PASSWD:
  454. if (!PasswdBuffer.empty()) {
  455. formerString=HiddenPasswdBuffer;
  456. PasswdBuffer.erase(--PasswdBuffer.end());
  457. HiddenPasswdBuffer.erase(--HiddenPasswdBuffer.end());
  458. }
  459. break;
  460. }
  461. }
  462. bool Panel::OnKeyPress(XEvent& event) {
  463. char ascii;
  464. KeySym keysym;
  465. XComposeStatus compstatus;
  466. int xx = 0;
  467. int yy = 0;
  468. string text;
  469. string formerString = "";
  470. XLookupString(&event.xkey, &ascii, 1, &keysym, &compstatus);
  471. switch(keysym){
  472. case XK_F1:
  473. SwitchSession();
  474. return true;
  475. case XK_F11:
  476. /* Take a screenshot */
  477. system(cfg->getOption("screenshot_cmd").c_str());
  478. return true;
  479. case XK_Return:
  480. case XK_KP_Enter:
  481. if (field==Get_Name){
  482. /* Don't allow an empty username */
  483. if (NameBuffer.empty()) return true;
  484. if (NameBuffer==CONSOLE_STR){
  485. action = Console;
  486. } else if (NameBuffer==HALT_STR){
  487. action = Halt;
  488. } else if (NameBuffer==REBOOT_STR){
  489. action = Reboot;
  490. } else if (NameBuffer==SUSPEND_STR){
  491. action = Suspend;
  492. } else if (NameBuffer==EXIT_STR){
  493. action = Exit;
  494. } else{
  495. if (mode == Mode_DM)
  496. action = Login;
  497. else
  498. action = Lock;
  499. }
  500. };
  501. return false;
  502. default:
  503. break;
  504. };
  505. Cursor(HIDE);
  506. switch(keysym){
  507. case XK_Delete:
  508. case XK_BackSpace:
  509. EraseLastChar(formerString);
  510. break;
  511. case XK_w:
  512. case XK_u:
  513. if (reinterpret_cast<XKeyEvent&>(event).state & ControlMask) {
  514. switch(field) {
  515. case Get_Passwd:
  516. formerString = HiddenPasswdBuffer;
  517. HiddenPasswdBuffer.clear();
  518. PasswdBuffer.clear();
  519. break;
  520. case Get_Name:
  521. formerString = NameBuffer;
  522. NameBuffer.clear();
  523. break;
  524. }
  525. break;
  526. }
  527. case XK_h:
  528. if (reinterpret_cast<XKeyEvent&>(event).state & ControlMask) {
  529. EraseLastChar(formerString);
  530. break;
  531. }
  532. /* Deliberate fall-through */
  533. default:
  534. if (isprint(ascii) && (keysym < XK_Shift_L || keysym > XK_Hyper_R)){
  535. switch(field) {
  536. case GET_NAME:
  537. formerString=NameBuffer;
  538. if (NameBuffer.length() < INPUT_MAXLENGTH_NAME-1){
  539. NameBuffer.append(&ascii,1);
  540. };
  541. break;
  542. case GET_PASSWD:
  543. formerString=HiddenPasswdBuffer;
  544. if (PasswdBuffer.length() < INPUT_MAXLENGTH_PASSWD-1){
  545. PasswdBuffer.append(&ascii,1);
  546. HiddenPasswdBuffer.append("*");
  547. };
  548. break;
  549. };
  550. };
  551. break;
  552. };
  553. XGlyphInfo extents;
  554. XftDraw *draw = XftDrawCreate(Dpy, Win,
  555. DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr));
  556. switch(field) {
  557. case Get_Name:
  558. text = NameBuffer;
  559. xx = input_name_x;
  560. yy = input_name_y;
  561. break;
  562. case Get_Passwd:
  563. text = HiddenPasswdBuffer;
  564. xx = input_pass_x;
  565. yy = input_pass_y;
  566. break;
  567. }
  568. if (!formerString.empty()){
  569. const char* txth = "Wj"; /* get proper maximum height ? */
  570. XftTextExtents8(Dpy, font,
  571. reinterpret_cast<const XftChar8*>(txth), strlen(txth), &extents);
  572. int maxHeight = extents.height;
  573. XftTextExtents8(Dpy, font,
  574. reinterpret_cast<const XftChar8*>(formerString.c_str()),
  575. formerString.length(), &extents);
  576. int maxLength = extents.width;
  577. if (mode == Mode_Lock)
  578. ApplyBackground(Rectangle(input_pass_x - 3,
  579. input_pass_y - maxHeight - 3,
  580. maxLength + 6, maxHeight + 6));
  581. else
  582. XClearArea(Dpy, Win, xx - 3, yy-maxHeight - 3,
  583. maxLength + 6, maxHeight + 6, false);
  584. }
  585. if (!text.empty()) {
  586. SlimDrawString8 (draw, &inputcolor, font, xx, yy,
  587. text,
  588. &inputshadowcolor,
  589. inputShadowXOffset, inputShadowYOffset);
  590. }
  591. XftDrawDestroy (draw);
  592. Cursor(SHOW);
  593. return true;
  594. }
  595. /* Draw welcome and "enter username" message */
  596. void Panel::ShowText(){
  597. string cfgX, cfgY;
  598. XGlyphInfo extents;
  599. bool singleInputMode =
  600. input_name_x == input_pass_x &&
  601. input_name_y == input_pass_y;
  602. XftDraw *draw = XftDrawCreate(Dpy, Win,
  603. DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr));
  604. /* welcome message */
  605. XftTextExtents8(Dpy, welcomefont, (XftChar8*)welcome_message.c_str(),
  606. strlen(welcome_message.c_str()), &extents);
  607. cfgX = cfg->getOption("welcome_x");
  608. cfgY = cfg->getOption("welcome_y");
  609. int shadowXOffset = cfg->getIntOption("welcome_shadow_xoffset");
  610. int shadowYOffset = cfg->getIntOption("welcome_shadow_yoffset");
  611. welcome_x = Cfg::absolutepos(cfgX, image->Width(), extents.width);
  612. welcome_y = Cfg::absolutepos(cfgY, image->Height(), extents.height);
  613. if (welcome_x >= 0 && welcome_y >= 0) {
  614. SlimDrawString8 (draw, &welcomecolor, welcomefont,
  615. welcome_x, welcome_y,
  616. welcome_message,
  617. &welcomeshadowcolor, shadowXOffset, shadowYOffset);
  618. }
  619. /* Enter username-password message */
  620. string msg;
  621. if ((!singleInputMode|| field == Get_Passwd) && mode == Mode_DM) {
  622. msg = cfg->getOption("password_msg");
  623. XftTextExtents8(Dpy, enterfont, (XftChar8*)msg.c_str(),
  624. strlen(msg.c_str()), &extents);
  625. cfgX = cfg->getOption("password_x");
  626. cfgY = cfg->getOption("password_y");
  627. int shadowXOffset = cfg->getIntOption("username_shadow_xoffset");
  628. int shadowYOffset = cfg->getIntOption("username_shadow_yoffset");
  629. password_x = Cfg::absolutepos(cfgX, image->Width(), extents.width);
  630. password_y = Cfg::absolutepos(cfgY, image->Height(), extents.height);
  631. if (password_x >= 0 && password_y >= 0){
  632. SlimDrawString8 (draw, &entercolor, enterfont, password_x, password_y,
  633. msg, &entershadowcolor, shadowXOffset, shadowYOffset);
  634. }
  635. }
  636. if (!singleInputMode|| field == Get_Name) {
  637. msg = cfg->getOption("username_msg");
  638. XftTextExtents8(Dpy, enterfont, (XftChar8*)msg.c_str(),
  639. strlen(msg.c_str()), &extents);
  640. cfgX = cfg->getOption("username_x");
  641. cfgY = cfg->getOption("username_y");
  642. int shadowXOffset = cfg->getIntOption("username_shadow_xoffset");
  643. int shadowYOffset = cfg->getIntOption("username_shadow_yoffset");
  644. username_x = Cfg::absolutepos(cfgX, image->Width(), extents.width);
  645. username_y = Cfg::absolutepos(cfgY, image->Height(), extents.height);
  646. if (username_x >= 0 && username_y >= 0){
  647. SlimDrawString8 (draw, &entercolor, enterfont, username_x, username_y,
  648. msg, &entershadowcolor, shadowXOffset, shadowYOffset);
  649. }
  650. }
  651. XftDrawDestroy(draw);
  652. if (mode == Mode_Lock) {
  653. // If only the password box is visible, draw the user name somewhere too
  654. string user_msg = "User: " + GetName();
  655. int show_username = cfg->getIntOption("show_username");
  656. if (singleInputMode && show_username) {
  657. Message(user_msg);
  658. }
  659. }
  660. }
  661. string Panel::getSession() {
  662. return session_exec;
  663. }
  664. /* choose next available session type */
  665. void Panel::SwitchSession() {
  666. pair<string,string> ses = cfg->nextSession();
  667. session_name = ses.first;
  668. session_exec = ses.second;
  669. if (session_name.size() > 0) {
  670. ShowSession();
  671. }
  672. }
  673. /* Display session type on the screen */
  674. void Panel::ShowSession() {
  675. string msg_x, msg_y;
  676. XClearWindow(Dpy, Root);
  677. string currsession = cfg->getOption("session_msg") + " " + session_name;
  678. XGlyphInfo extents;
  679. sessionfont = XftFontOpenName(Dpy, Scr, cfg->getOption("session_font").c_str());
  680. XftDraw *draw = XftDrawCreate(Dpy, Root,
  681. DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr));
  682. XftTextExtents8(Dpy, sessionfont, reinterpret_cast<const XftChar8*>(currsession.c_str()),
  683. currsession.length(), &extents);
  684. msg_x = cfg->getOption("session_x");
  685. msg_y = cfg->getOption("session_y");
  686. int x = Cfg::absolutepos(msg_x, XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), extents.width);
  687. int y = Cfg::absolutepos(msg_y, XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)), extents.height);
  688. int shadowXOffset = cfg->getIntOption("session_shadow_xoffset");
  689. int shadowYOffset = cfg->getIntOption("session_shadow_yoffset");
  690. SlimDrawString8(draw, &sessioncolor, sessionfont, x, y,
  691. currsession,
  692. &sessionshadowcolor,
  693. shadowXOffset, shadowYOffset);
  694. XFlush(Dpy);
  695. XftDrawDestroy(draw);
  696. }
  697. void Panel::SlimDrawString8(XftDraw *d, XftColor *color, XftFont *font,
  698. int x, int y, const string& str,
  699. XftColor* shadowColor,
  700. int xOffset, int yOffset)
  701. {
  702. int calc_x = 0;
  703. int calc_y = 0;
  704. if (mode == Mode_Lock) {
  705. calc_x = viewport.x;
  706. calc_y = viewport.y;
  707. }
  708. if (xOffset && yOffset) {
  709. XftDrawStringUtf8(d, shadowColor, font,
  710. x + xOffset + calc_x,
  711. y + yOffset + calc_y,
  712. reinterpret_cast<const FcChar8*>(str.c_str()),
  713. str.length());
  714. }
  715. XftDrawStringUtf8(d, color, font,
  716. x + calc_x,
  717. y + calc_y,
  718. reinterpret_cast<const FcChar8*>(str.c_str()),
  719. str.length());
  720. }
  721. Panel::ActionType Panel::getAction(void) const{
  722. return action;
  723. }
  724. void Panel::Reset(void){
  725. ResetName();
  726. ResetPasswd();
  727. }
  728. void Panel::ResetName(void){
  729. NameBuffer.clear();
  730. }
  731. void Panel::ResetPasswd(void){
  732. PasswdBuffer.clear();
  733. HiddenPasswdBuffer.clear();
  734. }
  735. void Panel::SetName(const string& name){
  736. NameBuffer=name;
  737. if (mode == Mode_DM)
  738. action = Login;
  739. else
  740. action = Lock;
  741. }
  742. const string& Panel::GetName(void) const{
  743. return NameBuffer;
  744. }
  745. const string& Panel::GetPasswd(void) const{
  746. return PasswdBuffer;
  747. }
  748. Rectangle Panel::GetPrimaryViewport() {
  749. Rectangle fallback;
  750. Rectangle result;
  751. RROutput primary;
  752. XRROutputInfo *primary_info;
  753. XRRScreenResources *resources;
  754. XRRCrtcInfo *crtc_info;
  755. int crtc;
  756. fallback.x = 0;
  757. fallback.y = 0;
  758. fallback.width = DisplayWidth(Dpy, Scr);
  759. fallback.height = DisplayHeight(Dpy, Scr);
  760. primary = XRRGetOutputPrimary(Dpy, Win);
  761. if (!primary) {
  762. return fallback;
  763. }
  764. resources = XRRGetScreenResources(Dpy, Win);
  765. if (!resources)
  766. return fallback;
  767. primary_info = XRRGetOutputInfo(Dpy, resources, primary);
  768. if (!primary_info) {
  769. XRRFreeScreenResources(resources);
  770. return fallback;
  771. }
  772. // Fixes bug with multiple monitors. Just pick first monitor if
  773. // XRRGetOutputInfo gives returns bad into for crtc.
  774. if (primary_info->crtc < 1) {
  775. if (primary_info->ncrtc > 0) {
  776. crtc = primary_info->crtcs[0];
  777. } else {
  778. cerr << "Cannot get crtc from xrandr.\n";
  779. exit(EXIT_FAILURE);
  780. }
  781. } else {
  782. crtc = primary_info->crtc;
  783. }
  784. crtc_info = XRRGetCrtcInfo(Dpy, resources, crtc);
  785. if (!crtc_info) {
  786. XRRFreeOutputInfo(primary_info);
  787. XRRFreeScreenResources(resources);
  788. return fallback;
  789. }
  790. result.x = crtc_info->x;
  791. result.y = crtc_info->y;
  792. result.width = crtc_info->width;
  793. result.height = crtc_info->height;
  794. XRRFreeCrtcInfo(crtc_info);
  795. XRRFreeOutputInfo(primary_info);
  796. XRRFreeScreenResources(resources);
  797. return result;
  798. }
  799. void Panel::ApplyBackground(Rectangle rect) {
  800. int ret = 0;
  801. if (rect.is_empty()) {
  802. rect.x = 0;
  803. rect.y = 0;
  804. rect.width = viewport.width;
  805. rect.height = viewport.height;
  806. }
  807. ret = XCopyArea(Dpy, PanelPixmap, Win, WinGC,
  808. rect.x, rect.y, rect.width, rect.height,
  809. viewport.x + rect.x, viewport.y + rect.y);
  810. if (!ret)
  811. cerr << APPNAME << ": failed to put pixmap on the screen\n.";
  812. }