panel.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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. bool Panel::OnKeyPress(XEvent& event) {
  446. char ascii;
  447. KeySym keysym;
  448. XComposeStatus compstatus;
  449. int xx = 0;
  450. int yy = 0;
  451. string text;
  452. string formerString = "";
  453. XLookupString(&event.xkey, &ascii, 1, &keysym, &compstatus);
  454. switch(keysym){
  455. case XK_F1:
  456. SwitchSession();
  457. return true;
  458. case XK_F11:
  459. /* Take a screenshot */
  460. system(cfg->getOption("screenshot_cmd").c_str());
  461. return true;
  462. case XK_Return:
  463. case XK_KP_Enter:
  464. if (field==Get_Name){
  465. /* Don't allow an empty username */
  466. if (NameBuffer.empty()) return true;
  467. if (NameBuffer==CONSOLE_STR){
  468. action = Console;
  469. } else if (NameBuffer==HALT_STR){
  470. action = Halt;
  471. } else if (NameBuffer==REBOOT_STR){
  472. action = Reboot;
  473. } else if (NameBuffer==SUSPEND_STR){
  474. action = Suspend;
  475. } else if (NameBuffer==EXIT_STR){
  476. action = Exit;
  477. } else{
  478. if (mode == Mode_DM)
  479. action = Login;
  480. else
  481. action = Lock;
  482. }
  483. };
  484. return false;
  485. default:
  486. break;
  487. };
  488. Cursor(HIDE);
  489. switch(keysym){
  490. case XK_Delete:
  491. case XK_BackSpace:
  492. switch(field) {
  493. case GET_NAME:
  494. if (! NameBuffer.empty()){
  495. formerString=NameBuffer;
  496. NameBuffer.erase(--NameBuffer.end());
  497. };
  498. break;
  499. case GET_PASSWD:
  500. if (! PasswdBuffer.empty()){
  501. formerString=HiddenPasswdBuffer;
  502. PasswdBuffer.erase(--PasswdBuffer.end());
  503. HiddenPasswdBuffer.erase(--HiddenPasswdBuffer.end());
  504. };
  505. break;
  506. };
  507. break;
  508. case XK_w:
  509. case XK_u:
  510. if (reinterpret_cast<XKeyEvent&>(event).state & ControlMask) {
  511. switch(field) {
  512. case Get_Passwd:
  513. formerString = HiddenPasswdBuffer;
  514. HiddenPasswdBuffer.clear();
  515. PasswdBuffer.clear();
  516. break;
  517. case Get_Name:
  518. formerString = NameBuffer;
  519. NameBuffer.clear();
  520. break;
  521. };
  522. break;
  523. }
  524. /* Deliberate fall-through */
  525. default:
  526. if (isprint(ascii) && (keysym < XK_Shift_L || keysym > XK_Hyper_R)){
  527. switch(field) {
  528. case GET_NAME:
  529. formerString=NameBuffer;
  530. if (NameBuffer.length() < INPUT_MAXLENGTH_NAME-1){
  531. NameBuffer.append(&ascii,1);
  532. };
  533. break;
  534. case GET_PASSWD:
  535. formerString=HiddenPasswdBuffer;
  536. if (PasswdBuffer.length() < INPUT_MAXLENGTH_PASSWD-1){
  537. PasswdBuffer.append(&ascii,1);
  538. HiddenPasswdBuffer.append("*");
  539. };
  540. break;
  541. };
  542. };
  543. break;
  544. };
  545. XGlyphInfo extents;
  546. XftDraw *draw = XftDrawCreate(Dpy, Win,
  547. DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr));
  548. switch(field) {
  549. case Get_Name:
  550. text = NameBuffer;
  551. xx = input_name_x;
  552. yy = input_name_y;
  553. break;
  554. case Get_Passwd:
  555. text = HiddenPasswdBuffer;
  556. xx = input_pass_x;
  557. yy = input_pass_y;
  558. break;
  559. }
  560. if (!formerString.empty()){
  561. const char* txth = "Wj"; /* get proper maximum height ? */
  562. XftTextExtents8(Dpy, font,
  563. reinterpret_cast<const XftChar8*>(txth), strlen(txth), &extents);
  564. int maxHeight = extents.height;
  565. XftTextExtents8(Dpy, font,
  566. reinterpret_cast<const XftChar8*>(formerString.c_str()),
  567. formerString.length(), &extents);
  568. int maxLength = extents.width;
  569. if (mode == Mode_Lock)
  570. ApplyBackground(Rectangle(input_pass_x - 3,
  571. input_pass_y - maxHeight - 3,
  572. maxLength + 6, maxHeight + 6));
  573. else
  574. XClearArea(Dpy, Win, xx - 3, yy-maxHeight - 3,
  575. maxLength + 6, maxHeight + 6, false);
  576. }
  577. if (!text.empty()) {
  578. SlimDrawString8 (draw, &inputcolor, font, xx, yy,
  579. text,
  580. &inputshadowcolor,
  581. inputShadowXOffset, inputShadowYOffset);
  582. }
  583. XftDrawDestroy (draw);
  584. Cursor(SHOW);
  585. return true;
  586. }
  587. /* Draw welcome and "enter username" message */
  588. void Panel::ShowText(){
  589. string cfgX, cfgY;
  590. XGlyphInfo extents;
  591. bool singleInputMode =
  592. input_name_x == input_pass_x &&
  593. input_name_y == input_pass_y;
  594. XftDraw *draw = XftDrawCreate(Dpy, Win,
  595. DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr));
  596. /* welcome message */
  597. XftTextExtents8(Dpy, welcomefont, (XftChar8*)welcome_message.c_str(),
  598. strlen(welcome_message.c_str()), &extents);
  599. cfgX = cfg->getOption("welcome_x");
  600. cfgY = cfg->getOption("welcome_y");
  601. int shadowXOffset = cfg->getIntOption("welcome_shadow_xoffset");
  602. int shadowYOffset = cfg->getIntOption("welcome_shadow_yoffset");
  603. welcome_x = Cfg::absolutepos(cfgX, image->Width(), extents.width);
  604. welcome_y = Cfg::absolutepos(cfgY, image->Height(), extents.height);
  605. if (welcome_x >= 0 && welcome_y >= 0) {
  606. SlimDrawString8 (draw, &welcomecolor, welcomefont,
  607. welcome_x, welcome_y,
  608. welcome_message,
  609. &welcomeshadowcolor, shadowXOffset, shadowYOffset);
  610. }
  611. /* Enter username-password message */
  612. string msg;
  613. if ((!singleInputMode|| field == Get_Passwd) && mode == Mode_DM) {
  614. msg = cfg->getOption("password_msg");
  615. XftTextExtents8(Dpy, enterfont, (XftChar8*)msg.c_str(),
  616. strlen(msg.c_str()), &extents);
  617. cfgX = cfg->getOption("password_x");
  618. cfgY = cfg->getOption("password_y");
  619. int shadowXOffset = cfg->getIntOption("username_shadow_xoffset");
  620. int shadowYOffset = cfg->getIntOption("username_shadow_yoffset");
  621. password_x = Cfg::absolutepos(cfgX, image->Width(), extents.width);
  622. password_y = Cfg::absolutepos(cfgY, image->Height(), extents.height);
  623. if (password_x >= 0 && password_y >= 0){
  624. SlimDrawString8 (draw, &entercolor, enterfont, password_x, password_y,
  625. msg, &entershadowcolor, shadowXOffset, shadowYOffset);
  626. }
  627. }
  628. if (!singleInputMode|| field == Get_Name) {
  629. msg = cfg->getOption("username_msg");
  630. XftTextExtents8(Dpy, enterfont, (XftChar8*)msg.c_str(),
  631. strlen(msg.c_str()), &extents);
  632. cfgX = cfg->getOption("username_x");
  633. cfgY = cfg->getOption("username_y");
  634. int shadowXOffset = cfg->getIntOption("username_shadow_xoffset");
  635. int shadowYOffset = cfg->getIntOption("username_shadow_yoffset");
  636. username_x = Cfg::absolutepos(cfgX, image->Width(), extents.width);
  637. username_y = Cfg::absolutepos(cfgY, image->Height(), extents.height);
  638. if (username_x >= 0 && username_y >= 0){
  639. SlimDrawString8 (draw, &entercolor, enterfont, username_x, username_y,
  640. msg, &entershadowcolor, shadowXOffset, shadowYOffset);
  641. }
  642. }
  643. XftDrawDestroy(draw);
  644. if (mode == Mode_Lock) {
  645. // If only the password box is visible, draw the user name somewhere too
  646. string user_msg = "User: " + GetName();
  647. int show_username = cfg->getIntOption("show_username");
  648. if (singleInputMode && show_username) {
  649. Message(user_msg);
  650. }
  651. }
  652. }
  653. string Panel::getSession() {
  654. return session_exec;
  655. }
  656. /* choose next available session type */
  657. void Panel::SwitchSession() {
  658. pair<string,string> ses = cfg->nextSession();
  659. session_name = ses.first;
  660. session_exec = ses.second;
  661. if (session_name.size() > 0) {
  662. ShowSession();
  663. }
  664. }
  665. /* Display session type on the screen */
  666. void Panel::ShowSession() {
  667. string msg_x, msg_y;
  668. XClearWindow(Dpy, Root);
  669. string currsession = cfg->getOption("session_msg") + " " + session_name;
  670. XGlyphInfo extents;
  671. sessionfont = XftFontOpenName(Dpy, Scr, cfg->getOption("session_font").c_str());
  672. XftDraw *draw = XftDrawCreate(Dpy, Root,
  673. DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr));
  674. XftTextExtents8(Dpy, sessionfont, reinterpret_cast<const XftChar8*>(currsession.c_str()),
  675. currsession.length(), &extents);
  676. msg_x = cfg->getOption("session_x");
  677. msg_y = cfg->getOption("session_y");
  678. int x = Cfg::absolutepos(msg_x, XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), extents.width);
  679. int y = Cfg::absolutepos(msg_y, XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)), extents.height);
  680. int shadowXOffset = cfg->getIntOption("session_shadow_xoffset");
  681. int shadowYOffset = cfg->getIntOption("session_shadow_yoffset");
  682. SlimDrawString8(draw, &sessioncolor, sessionfont, x, y,
  683. currsession,
  684. &sessionshadowcolor,
  685. shadowXOffset, shadowYOffset);
  686. XFlush(Dpy);
  687. XftDrawDestroy(draw);
  688. }
  689. void Panel::SlimDrawString8(XftDraw *d, XftColor *color, XftFont *font,
  690. int x, int y, const string& str,
  691. XftColor* shadowColor,
  692. int xOffset, int yOffset)
  693. {
  694. int calc_x = 0;
  695. int calc_y = 0;
  696. if (mode == Mode_Lock) {
  697. calc_x = viewport.x;
  698. calc_y = viewport.y;
  699. }
  700. if (xOffset && yOffset) {
  701. XftDrawStringUtf8(d, shadowColor, font,
  702. x + xOffset + calc_x,
  703. y + yOffset + calc_y,
  704. reinterpret_cast<const FcChar8*>(str.c_str()),
  705. str.length());
  706. }
  707. XftDrawStringUtf8(d, color, font,
  708. x + calc_x,
  709. y + calc_y,
  710. reinterpret_cast<const FcChar8*>(str.c_str()),
  711. str.length());
  712. }
  713. Panel::ActionType Panel::getAction(void) const{
  714. return action;
  715. }
  716. void Panel::Reset(void){
  717. ResetName();
  718. ResetPasswd();
  719. }
  720. void Panel::ResetName(void){
  721. NameBuffer.clear();
  722. }
  723. void Panel::ResetPasswd(void){
  724. PasswdBuffer.clear();
  725. HiddenPasswdBuffer.clear();
  726. }
  727. void Panel::SetName(const string& name){
  728. NameBuffer=name;
  729. if (mode == Mode_DM)
  730. action = Login;
  731. else
  732. action = Lock;
  733. }
  734. const string& Panel::GetName(void) const{
  735. return NameBuffer;
  736. }
  737. const string& Panel::GetPasswd(void) const{
  738. return PasswdBuffer;
  739. }
  740. Rectangle Panel::GetPrimaryViewport() {
  741. Rectangle fallback;
  742. Rectangle result;
  743. RROutput primary;
  744. XRROutputInfo *primary_info;
  745. XRRScreenResources *resources;
  746. XRRCrtcInfo *crtc_info;
  747. fallback.x = 0;
  748. fallback.y = 0;
  749. fallback.width = DisplayWidth(Dpy, Scr);
  750. fallback.height = DisplayHeight(Dpy, Scr);
  751. primary = XRRGetOutputPrimary(Dpy, Win);
  752. if (!primary) {
  753. return fallback;
  754. }
  755. resources = XRRGetScreenResources(Dpy, Win);
  756. if (!resources)
  757. return fallback;
  758. primary_info = XRRGetOutputInfo(Dpy, resources, primary);
  759. if (!primary_info) {
  760. XRRFreeScreenResources(resources);
  761. return fallback;
  762. }
  763. crtc_info = XRRGetCrtcInfo(Dpy, resources, primary_info->crtc);
  764. if (!crtc_info) {
  765. XRRFreeOutputInfo(primary_info);
  766. XRRFreeScreenResources(resources);
  767. return fallback;
  768. }
  769. result.x = crtc_info->x;
  770. result.y = crtc_info->y;
  771. result.width = crtc_info->width;
  772. result.height = crtc_info->height;
  773. XRRFreeCrtcInfo(crtc_info);
  774. XRRFreeOutputInfo(primary_info);
  775. XRRFreeScreenResources(resources);
  776. return result;
  777. }
  778. void Panel::ApplyBackground(Rectangle rect) {
  779. int ret = 0;
  780. if (rect.is_empty()) {
  781. rect.x = 0;
  782. rect.y = 0;
  783. rect.width = viewport.width;
  784. rect.height = viewport.height;
  785. }
  786. ret = XCopyArea(Dpy, PanelPixmap, Win, WinGC,
  787. rect.x, rect.y, rect.width, rect.height,
  788. viewport.x + rect.x, viewport.y + rect.y);
  789. if (!ret)
  790. cerr << APPNAME << ": failed to put pixmap on the screen\n.";
  791. }