panel.cpp 25 KB

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