panel.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 "panel.h"
  13. using namespace std;
  14. Panel::Panel(Display* dpy, int scr, Window root, Cfg* config,
  15. const string& themedir) {
  16. // Set display
  17. Dpy = dpy;
  18. Scr = scr;
  19. Root = root;
  20. cfg = config;
  21. session = "";
  22. // Init GC
  23. XGCValues gcv;
  24. unsigned long gcm;
  25. gcm = GCForeground|GCBackground|GCGraphicsExposures;
  26. gcv.foreground = GetColor("black");
  27. gcv.background = GetColor("white");
  28. gcv.graphics_exposures = False;
  29. TextGC = XCreateGC(Dpy, Root, gcm, &gcv);
  30. font = XftFontOpenName(Dpy, Scr, cfg->getOption("input_font").c_str());
  31. welcomefont = XftFontOpenName(Dpy, Scr, cfg->getOption("welcome_font").c_str());
  32. introfont = XftFontOpenName(Dpy, Scr, cfg->getOption("intro_font").c_str());
  33. enterfont = XftFontOpenName(Dpy, Scr, cfg->getOption("username_font").c_str());
  34. msgfont = XftFontOpenName(Dpy, Scr, cfg->getOption("msg_font").c_str());
  35. Visual* visual = DefaultVisual(Dpy, Scr);
  36. Colormap colormap = DefaultColormap(Dpy, Scr);
  37. // NOTE: using XftColorAllocValue() would be a better solution. Lazy me.
  38. XftColorAllocName(Dpy, visual, colormap, cfg->getOption("input_color").c_str(), &inputcolor);
  39. XftColorAllocName(Dpy, visual, colormap, cfg->getOption("input_shadow_color").c_str(), &inputshadowcolor);
  40. XftColorAllocName(Dpy, visual, colormap, cfg->getOption("welcome_color").c_str(), &welcomecolor);
  41. XftColorAllocName(Dpy, visual, colormap, cfg->getOption("welcome_shadow_color").c_str(), &welcomeshadowcolor);
  42. XftColorAllocName(Dpy, visual, colormap, cfg->getOption("username_color").c_str(), &entercolor);
  43. XftColorAllocName(Dpy, visual, colormap, cfg->getOption("username_shadow_color").c_str(), &entershadowcolor);
  44. XftColorAllocName(Dpy, visual, colormap, cfg->getOption("msg_color").c_str(), &msgcolor);
  45. XftColorAllocName(Dpy, visual, colormap, cfg->getOption("msg_shadow_color").c_str(), &msgshadowcolor);
  46. XftColorAllocName(Dpy, visual, colormap, cfg->getOption("intro_color").c_str(), &introcolor);
  47. XftColorAllocName(Dpy, DefaultVisual(Dpy, Scr), colormap,
  48. cfg->getOption("session_color").c_str(), &sessioncolor);
  49. XftColorAllocName(Dpy, DefaultVisual(Dpy, Scr), colormap,
  50. cfg->getOption("session_shadow_color").c_str(), &sessionshadowcolor);
  51. // Load properties from config / theme
  52. input_name_x = cfg->getIntOption("input_name_x");
  53. input_name_y = cfg->getIntOption("input_name_y");
  54. input_pass_x = cfg->getIntOption("input_pass_x");
  55. input_pass_y = cfg->getIntOption("input_pass_y");
  56. inputShadowXOffset = cfg->getIntOption("input_shadow_xoffset");
  57. inputShadowYOffset = cfg->getIntOption("input_shadow_yoffset");
  58. if (input_pass_x < 0 || input_pass_y < 0){ // single inputbox mode
  59. input_pass_x = input_name_x;
  60. input_pass_y = input_name_y;
  61. }
  62. // Load panel and background image
  63. string panelpng = "";
  64. panelpng = panelpng + themedir +"/panel.png";
  65. image = new Image;
  66. bool loaded = image->Read(panelpng.c_str());
  67. if (!loaded) { // try jpeg if png failed
  68. panelpng = themedir + "/panel.jpg";
  69. loaded = image->Read(panelpng.c_str());
  70. if (!loaded) {
  71. logStream << APPNAME
  72. << ": could not load panel image for theme '"
  73. << basename((char*)themedir.c_str()) << "'"
  74. << endl;
  75. exit(ERR_EXIT);
  76. }
  77. }
  78. Image* bg = new Image();
  79. string bgstyle = cfg->getOption("background_style");
  80. if (bgstyle != "color") {
  81. panelpng = themedir +"/background.png";
  82. loaded = bg->Read(panelpng.c_str());
  83. if (!loaded) { // try jpeg if png failed
  84. panelpng = themedir + "/background.jpg";
  85. loaded = bg->Read(panelpng.c_str());
  86. if (!loaded){
  87. logStream << APPNAME
  88. << ": could not load background image for theme '"
  89. << basename((char*)themedir.c_str()) << "'"
  90. << endl;
  91. exit(ERR_EXIT);
  92. }
  93. }
  94. }
  95. if (bgstyle == "stretch") {
  96. bg->Resize(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)));
  97. } else if (bgstyle == "tile") {
  98. bg->Tile(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)));
  99. } else if (bgstyle == "center") {
  100. string hexvalue = cfg->getOption("background_color");
  101. hexvalue = hexvalue.substr(1,6);
  102. bg->Center(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)),
  103. XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)),
  104. hexvalue.c_str());
  105. } else { // plain color or error
  106. string hexvalue = cfg->getOption("background_color");
  107. hexvalue = hexvalue.substr(1,6);
  108. bg->Center(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)),
  109. XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)),
  110. hexvalue.c_str());
  111. }
  112. string cfgX = cfg->getOption("input_panel_x");
  113. string cfgY = cfg->getOption("input_panel_y");
  114. X = Cfg::absolutepos(cfgX, XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), image->Width());
  115. Y = Cfg::absolutepos(cfgY, XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)), image->Height());
  116. // Merge image into background
  117. image->Merge(bg, X, Y);
  118. delete bg;
  119. PanelPixmap = image->createPixmap(Dpy, Scr, Root);
  120. // Read (and substitute vars in) the welcome message
  121. welcome_message = cfg->getWelcomeMessage();
  122. intro_message = cfg->getOption("intro_msg");
  123. }
  124. Panel::~Panel() {
  125. XftColorFree (Dpy, DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr), &inputcolor);
  126. XftColorFree (Dpy, DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr), &msgcolor);
  127. XftColorFree (Dpy, DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr), &welcomecolor);
  128. XftColorFree (Dpy, DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr), &entercolor);
  129. XftColorFree (Dpy, DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr), &sessioncolor);
  130. XftColorFree (Dpy, DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr), &sessionshadowcolor);
  131. XFreeGC(Dpy, TextGC);
  132. XftFontClose(Dpy, font);
  133. XftFontClose(Dpy, msgfont);
  134. XftFontClose(Dpy, introfont);
  135. XftFontClose(Dpy, welcomefont);
  136. XftFontClose(Dpy, enterfont);
  137. delete image;
  138. }
  139. void Panel::OpenPanel() {
  140. // Create window
  141. Win = XCreateSimpleWindow(Dpy, Root, X, Y,
  142. image->Width(),
  143. image->Height(),
  144. 0, GetColor("white"), GetColor("white"));
  145. // Events
  146. XSelectInput(Dpy, Win, ExposureMask | KeyPressMask);
  147. // Set background
  148. XSetWindowBackgroundPixmap(Dpy, Win, PanelPixmap);
  149. // Show window
  150. XMapWindow(Dpy, Win);
  151. XMoveWindow(Dpy, Win, X, Y); // override wm positioning (for tests)
  152. // Grab keyboard
  153. XGrabKeyboard(Dpy, Win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
  154. XFlush(Dpy);
  155. }
  156. void Panel::ClosePanel() {
  157. XUngrabKeyboard(Dpy, CurrentTime);
  158. XUnmapWindow(Dpy, Win);
  159. XDestroyWindow(Dpy, Win);
  160. XFlush(Dpy);
  161. }
  162. void Panel::ClearPanel() {
  163. session = "";
  164. Reset();
  165. XClearWindow(Dpy, Root);
  166. XClearWindow(Dpy, Win);
  167. Cursor(SHOW);
  168. ShowText();
  169. XFlush(Dpy);
  170. }
  171. void Panel::Message(const string& text) {
  172. string cfgX, cfgY;
  173. XGlyphInfo extents;
  174. XftDraw *draw = XftDrawCreate(Dpy, Root,
  175. DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr));
  176. XftTextExtents8(Dpy, msgfont, reinterpret_cast<const XftChar8*>(text.c_str()),
  177. text.length(), &extents);
  178. cfgX = cfg->getOption("msg_x");
  179. cfgY = cfg->getOption("msg_y");
  180. int shadowXOffset = cfg->getIntOption("msg_shadow_xoffset");
  181. int shadowYOffset = cfg->getIntOption("msg_shadow_yoffset");
  182. int msg_x = Cfg::absolutepos(cfgX, XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), extents.width);
  183. int msg_y = Cfg::absolutepos(cfgY, XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)), extents.height);
  184. SlimDrawString8 (draw, &msgcolor, msgfont, msg_x, msg_y,
  185. text,
  186. &msgshadowcolor,
  187. shadowXOffset, shadowYOffset);
  188. XFlush(Dpy);
  189. XftDrawDestroy(draw);
  190. }
  191. void Panel::Error(const string& text) {
  192. ClosePanel();
  193. Message(text);
  194. sleep(ERROR_DURATION);
  195. OpenPanel();
  196. ClearPanel();
  197. }
  198. unsigned long Panel::GetColor(const char* colorname) {
  199. XColor color;
  200. XWindowAttributes attributes;
  201. XGetWindowAttributes(Dpy, Root, &attributes);
  202. color.pixel = 0;
  203. if(!XParseColor(Dpy, attributes.colormap, colorname, &color))
  204. logStream << APPNAME << ": can't parse color " << colorname << endl;
  205. else if(!XAllocColor(Dpy, attributes.colormap, &color))
  206. logStream << APPNAME << ": can't allocate color " << colorname << endl;
  207. return color.pixel;
  208. }
  209. void Panel::Cursor(int visible) {
  210. const char* text;
  211. int xx, yy, y2, cheight;
  212. const char* txth = "Wj"; // used to get cursor height
  213. switch(field) {
  214. case Get_Passwd:
  215. text = HiddenPasswdBuffer.c_str();
  216. xx = input_pass_x;
  217. yy = input_pass_y;
  218. break;
  219. case Get_Name:
  220. text = NameBuffer.c_str();
  221. xx = input_name_x;
  222. yy = input_name_y;
  223. break;
  224. }
  225. XGlyphInfo extents;
  226. XftTextExtents8(Dpy, font, (XftChar8*)txth, strlen(txth), &extents);
  227. cheight = extents.height;
  228. y2 = yy - extents.y + extents.height;
  229. XftTextExtents8(Dpy, font, (XftChar8*)text, strlen(text), &extents);
  230. xx += extents.width;
  231. if(visible == SHOW) {
  232. XSetForeground(Dpy, TextGC,
  233. GetColor(cfg->getOption("input_color").c_str()));
  234. XDrawLine(Dpy, Win, TextGC,
  235. xx+1, yy-cheight,
  236. xx+1, y2);
  237. } else {
  238. XClearArea(Dpy, Win, xx+1, yy-cheight,
  239. 1, y2-(yy-cheight)+1, false);
  240. }
  241. }
  242. void Panel::EventHandler(const Panel::FieldType& curfield) {
  243. XEvent event;
  244. field=curfield;
  245. bool loop = true;
  246. OnExpose();
  247. struct pollfd x11_pfd = {0};
  248. x11_pfd.fd = ConnectionNumber(Dpy);
  249. x11_pfd.events = POLLIN;
  250. while(loop) {
  251. if(XPending(Dpy) || poll(&x11_pfd, 1, -1) > 0) {
  252. while(XPending(Dpy)) {
  253. XNextEvent(Dpy, &event);
  254. switch(event.type) {
  255. case Expose:
  256. OnExpose();
  257. break;
  258. case KeyPress:
  259. loop=OnKeyPress(event);
  260. break;
  261. }
  262. }
  263. }
  264. }
  265. return;
  266. }
  267. void Panel::OnExpose(void) {
  268. XftDraw *draw = XftDrawCreate(Dpy, Win,
  269. DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr));
  270. XClearWindow(Dpy, Win);
  271. if (input_pass_x != input_name_x || input_pass_y != input_name_y){
  272. SlimDrawString8 (draw, &inputcolor, font, input_name_x, input_name_y,
  273. NameBuffer,
  274. &inputshadowcolor,
  275. inputShadowXOffset, inputShadowYOffset);
  276. SlimDrawString8 (draw, &inputcolor, font, input_pass_x, input_pass_y,
  277. HiddenPasswdBuffer,
  278. &inputshadowcolor,
  279. inputShadowXOffset, inputShadowYOffset);
  280. } else { //single input mode
  281. switch(field) {
  282. case Get_Passwd:
  283. SlimDrawString8 (draw, &inputcolor, font,
  284. input_pass_x, input_pass_y,
  285. HiddenPasswdBuffer,
  286. &inputshadowcolor,
  287. inputShadowXOffset, inputShadowYOffset);
  288. break;
  289. case Get_Name:
  290. SlimDrawString8 (draw, &inputcolor, font,
  291. input_name_x, input_name_y,
  292. NameBuffer,
  293. &inputshadowcolor,
  294. inputShadowXOffset, inputShadowYOffset);
  295. break;
  296. }
  297. }
  298. XftDrawDestroy (draw);
  299. Cursor(SHOW);
  300. ShowText();
  301. }
  302. bool Panel::OnKeyPress(XEvent& event) {
  303. char ascii;
  304. KeySym keysym;
  305. XComposeStatus compstatus;
  306. int xx;
  307. int yy;
  308. string text;
  309. string formerString = "";
  310. XLookupString(&event.xkey, &ascii, 1, &keysym, &compstatus);
  311. switch(keysym){
  312. case XK_F1:
  313. SwitchSession();
  314. return true;
  315. case XK_F11:
  316. // Take a screenshot
  317. system(cfg->getOption("screenshot_cmd").c_str());
  318. return true;
  319. case XK_Return:
  320. case XK_KP_Enter:
  321. if (field==Get_Name){
  322. // Don't allow an empty username
  323. if (NameBuffer.empty()) return true;
  324. if (NameBuffer==CONSOLE_STR){
  325. action = Console;
  326. } else if (NameBuffer==HALT_STR){
  327. action = Halt;
  328. } else if (NameBuffer==REBOOT_STR){
  329. action = Reboot;
  330. } else if (NameBuffer==SUSPEND_STR){
  331. action = Suspend;
  332. } else if (NameBuffer==EXIT_STR){
  333. action = Exit;
  334. } else{
  335. action = Login;
  336. }
  337. };
  338. return false;
  339. default:
  340. break;
  341. };
  342. Cursor(HIDE);
  343. switch(keysym){
  344. case XK_Delete:
  345. case XK_BackSpace:
  346. switch(field) {
  347. case GET_NAME:
  348. if (! NameBuffer.empty()){
  349. formerString=NameBuffer;
  350. NameBuffer.erase(--NameBuffer.end());
  351. };
  352. break;
  353. case GET_PASSWD:
  354. if (! PasswdBuffer.empty()){
  355. formerString=HiddenPasswdBuffer;
  356. PasswdBuffer.erase(--PasswdBuffer.end());
  357. HiddenPasswdBuffer.erase(--HiddenPasswdBuffer.end());
  358. };
  359. break;
  360. };
  361. break;
  362. case XK_w:
  363. case XK_u:
  364. if (reinterpret_cast<XKeyEvent&>(event).state & ControlMask) {
  365. switch(field) {
  366. case Get_Passwd:
  367. formerString = HiddenPasswdBuffer;
  368. HiddenPasswdBuffer.clear();
  369. PasswdBuffer.clear();
  370. break;
  371. case Get_Name:
  372. formerString = NameBuffer;
  373. NameBuffer.clear();
  374. break;
  375. };
  376. break;
  377. }
  378. // Deliberate fall-through
  379. default:
  380. if (isprint(ascii) && (keysym < XK_Shift_L || keysym > XK_Hyper_R)){
  381. switch(field) {
  382. case GET_NAME:
  383. formerString=NameBuffer;
  384. if (NameBuffer.length() < INPUT_MAXLENGTH_NAME-1){
  385. NameBuffer.append(&ascii,1);
  386. };
  387. break;
  388. case GET_PASSWD:
  389. formerString=HiddenPasswdBuffer;
  390. if (PasswdBuffer.length() < INPUT_MAXLENGTH_PASSWD-1){
  391. PasswdBuffer.append(&ascii,1);
  392. HiddenPasswdBuffer.append("*");
  393. };
  394. break;
  395. };
  396. };
  397. break;
  398. };
  399. XGlyphInfo extents;
  400. XftDraw *draw = XftDrawCreate(Dpy, Win,
  401. DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr));
  402. switch(field) {
  403. case Get_Name:
  404. text = NameBuffer;
  405. xx = input_name_x;
  406. yy = input_name_y;
  407. break;
  408. case Get_Passwd:
  409. text = HiddenPasswdBuffer;
  410. xx = input_pass_x;
  411. yy = input_pass_y;
  412. break;
  413. }
  414. if (!formerString.empty()){
  415. const char* txth = "Wj"; // get proper maximum height ?
  416. XftTextExtents8(Dpy, font, reinterpret_cast<const XftChar8*>(txth), strlen(txth), &extents);
  417. int maxHeight = extents.height;
  418. XftTextExtents8(Dpy, font, reinterpret_cast<const XftChar8*>(formerString.c_str()),
  419. formerString.length(), &extents);
  420. int maxLength = extents.width;
  421. XClearArea(Dpy, Win, xx-3, yy-maxHeight-3,
  422. maxLength+6, maxHeight+6, false);
  423. }
  424. if (!text.empty()) {
  425. SlimDrawString8 (draw, &inputcolor, font, xx, yy,
  426. text,
  427. &inputshadowcolor,
  428. inputShadowXOffset, inputShadowYOffset);
  429. }
  430. XftDrawDestroy (draw);
  431. Cursor(SHOW);
  432. return true;
  433. }
  434. // Draw welcome and "enter username" message
  435. void Panel::ShowText(){
  436. string cfgX, cfgY;
  437. XGlyphInfo extents;
  438. bool singleInputMode =
  439. input_name_x == input_pass_x &&
  440. input_name_y == input_pass_y;
  441. XftDraw *draw = XftDrawCreate(Dpy, Win,
  442. DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr));
  443. /* welcome message */
  444. XftTextExtents8(Dpy, welcomefont, (XftChar8*)welcome_message.c_str(),
  445. strlen(welcome_message.c_str()), &extents);
  446. cfgX = cfg->getOption("welcome_x");
  447. cfgY = cfg->getOption("welcome_y");
  448. int shadowXOffset = cfg->getIntOption("welcome_shadow_xoffset");
  449. int shadowYOffset = cfg->getIntOption("welcome_shadow_yoffset");
  450. welcome_x = Cfg::absolutepos(cfgX, image->Width(), extents.width);
  451. welcome_y = Cfg::absolutepos(cfgY, image->Height(), extents.height);
  452. if (welcome_x >= 0 && welcome_y >= 0) {
  453. SlimDrawString8 (draw, &welcomecolor, welcomefont,
  454. welcome_x, welcome_y,
  455. welcome_message,
  456. &welcomeshadowcolor, shadowXOffset, shadowYOffset);
  457. }
  458. /* Enter username-password message */
  459. string msg;
  460. if (!singleInputMode|| field == Get_Passwd ) {
  461. msg = cfg->getOption("password_msg");
  462. XftTextExtents8(Dpy, enterfont, (XftChar8*)msg.c_str(),
  463. strlen(msg.c_str()), &extents);
  464. cfgX = cfg->getOption("password_x");
  465. cfgY = cfg->getOption("password_y");
  466. int shadowXOffset = cfg->getIntOption("username_shadow_xoffset");
  467. int shadowYOffset = cfg->getIntOption("username_shadow_yoffset");
  468. password_x = Cfg::absolutepos(cfgX, image->Width(), extents.width);
  469. password_y = Cfg::absolutepos(cfgY, image->Height(), extents.height);
  470. if (password_x >= 0 && password_y >= 0){
  471. SlimDrawString8 (draw, &entercolor, enterfont, password_x, password_y,
  472. msg, &entershadowcolor, shadowXOffset, shadowYOffset);
  473. }
  474. }
  475. if (!singleInputMode|| field == Get_Name ) {
  476. msg = cfg->getOption("username_msg");
  477. XftTextExtents8(Dpy, enterfont, (XftChar8*)msg.c_str(),
  478. strlen(msg.c_str()), &extents);
  479. cfgX = cfg->getOption("username_x");
  480. cfgY = cfg->getOption("username_y");
  481. int shadowXOffset = cfg->getIntOption("username_shadow_xoffset");
  482. int shadowYOffset = cfg->getIntOption("username_shadow_yoffset");
  483. username_x = Cfg::absolutepos(cfgX, image->Width(), extents.width);
  484. username_y = Cfg::absolutepos(cfgY, image->Height(), extents.height);
  485. if (username_x >= 0 && username_y >= 0){
  486. SlimDrawString8 (draw, &entercolor, enterfont, username_x, username_y,
  487. msg, &entershadowcolor, shadowXOffset, shadowYOffset);
  488. }
  489. }
  490. XftDrawDestroy(draw);
  491. }
  492. string Panel::getSession() {
  493. return session;
  494. }
  495. // choose next available session type
  496. void Panel::SwitchSession() {
  497. session = cfg->nextSession(session);
  498. if (session.size() > 0) {
  499. ShowSession();
  500. }
  501. }
  502. // Display session type on the screen
  503. void Panel::ShowSession() {
  504. string msg_x, msg_y;
  505. XClearWindow(Dpy, Root);
  506. string currsession = cfg->getOption("session_msg") + " " + session;
  507. XGlyphInfo extents;
  508. sessionfont = XftFontOpenName(Dpy, Scr, cfg->getOption("session_font").c_str());
  509. XftDraw *draw = XftDrawCreate(Dpy, Root,
  510. DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr));
  511. XftTextExtents8(Dpy, sessionfont, reinterpret_cast<const XftChar8*>(currsession.c_str()),
  512. currsession.length(), &extents);
  513. msg_x = cfg->getOption("session_x");
  514. msg_y = cfg->getOption("session_y");
  515. int x = Cfg::absolutepos(msg_x, XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), extents.width);
  516. int y = Cfg::absolutepos(msg_y, XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)), extents.height);
  517. int shadowXOffset = cfg->getIntOption("session_shadow_xoffset");
  518. int shadowYOffset = cfg->getIntOption("session_shadow_yoffset");
  519. SlimDrawString8(draw, &sessioncolor, sessionfont, x, y,
  520. currsession,
  521. &sessionshadowcolor,
  522. shadowXOffset, shadowYOffset);
  523. XFlush(Dpy);
  524. XftDrawDestroy(draw);
  525. }
  526. void Panel::SlimDrawString8(XftDraw *d, XftColor *color, XftFont *font,
  527. int x, int y, const string& str,
  528. XftColor* shadowColor,
  529. int xOffset, int yOffset)
  530. {
  531. if (xOffset && yOffset) {
  532. XftDrawString8(d, shadowColor, font, x+xOffset, y+yOffset,
  533. reinterpret_cast<const FcChar8*>(str.c_str()), str.length());
  534. }
  535. XftDrawString8(d, color, font, x, y, reinterpret_cast<const FcChar8*>(str.c_str()), str.length());
  536. }
  537. Panel::ActionType Panel::getAction(void) const{
  538. return action;
  539. };
  540. void Panel::Reset(void){
  541. ResetName();
  542. ResetPasswd();
  543. };
  544. void Panel::ResetName(void){
  545. NameBuffer.clear();
  546. };
  547. void Panel::ResetPasswd(void){
  548. PasswdBuffer.clear();
  549. HiddenPasswdBuffer.clear();
  550. };
  551. void Panel::SetName(const string& name){
  552. NameBuffer=name;
  553. action = Login;
  554. };
  555. const string& Panel::GetName(void) const{
  556. return NameBuffer;
  557. };
  558. const string& Panel::GetPasswd(void) const{
  559. return PasswdBuffer;
  560. };