panel.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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. else {
  552. return true; //nodraw if notchange
  553. };
  554. break;
  555. };
  556. XGlyphInfo extents;
  557. XftDraw *draw = XftDrawCreate(Dpy, Win,
  558. DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr));
  559. switch(field) {
  560. case Get_Name:
  561. text = NameBuffer;
  562. xx = input_name_x;
  563. yy = input_name_y;
  564. break;
  565. case Get_Passwd:
  566. text = HiddenPasswdBuffer;
  567. xx = input_pass_x;
  568. yy = input_pass_y;
  569. break;
  570. }
  571. if (!formerString.empty()){
  572. const char* txth = "Wj"; /* get proper maximum height ? */
  573. XftTextExtents8(Dpy, font,
  574. reinterpret_cast<const XftChar8*>(txth), strlen(txth), &extents);
  575. int maxHeight = extents.height;
  576. XftTextExtents8(Dpy, font,
  577. reinterpret_cast<const XftChar8*>(formerString.c_str()),
  578. formerString.length(), &extents);
  579. int maxLength = extents.width;
  580. if (mode == Mode_Lock)
  581. ApplyBackground(Rectangle(input_pass_x - 3,
  582. input_pass_y - maxHeight - 3,
  583. maxLength + 6, maxHeight + 6));
  584. else
  585. XClearArea(Dpy, Win, xx - 3, yy-maxHeight - 3,
  586. maxLength + 6, maxHeight + 6, false);
  587. }
  588. if (!text.empty()) {
  589. SlimDrawString8 (draw, &inputcolor, font, xx, yy,
  590. text,
  591. &inputshadowcolor,
  592. inputShadowXOffset, inputShadowYOffset);
  593. }
  594. XftDrawDestroy (draw);
  595. Cursor(SHOW);
  596. return true;
  597. }
  598. /* Draw welcome and "enter username" message */
  599. void Panel::ShowText(){
  600. string cfgX, cfgY;
  601. XGlyphInfo extents;
  602. bool singleInputMode =
  603. input_name_x == input_pass_x &&
  604. input_name_y == input_pass_y;
  605. XftDraw *draw = XftDrawCreate(Dpy, Win,
  606. DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr));
  607. /* welcome message */
  608. XftTextExtents8(Dpy, welcomefont, (XftChar8*)welcome_message.c_str(),
  609. strlen(welcome_message.c_str()), &extents);
  610. cfgX = cfg->getOption("welcome_x");
  611. cfgY = cfg->getOption("welcome_y");
  612. int shadowXOffset = cfg->getIntOption("welcome_shadow_xoffset");
  613. int shadowYOffset = cfg->getIntOption("welcome_shadow_yoffset");
  614. welcome_x = Cfg::absolutepos(cfgX, image->Width(), extents.width);
  615. welcome_y = Cfg::absolutepos(cfgY, image->Height(), extents.height);
  616. if (welcome_x >= 0 && welcome_y >= 0) {
  617. SlimDrawString8 (draw, &welcomecolor, welcomefont,
  618. welcome_x, welcome_y,
  619. welcome_message,
  620. &welcomeshadowcolor, shadowXOffset, shadowYOffset);
  621. }
  622. /* Enter username-password message */
  623. string msg;
  624. if ((!singleInputMode|| field == Get_Passwd) && mode == Mode_DM) {
  625. msg = cfg->getOption("password_msg");
  626. XftTextExtents8(Dpy, enterfont, (XftChar8*)msg.c_str(),
  627. strlen(msg.c_str()), &extents);
  628. cfgX = cfg->getOption("password_x");
  629. cfgY = cfg->getOption("password_y");
  630. int shadowXOffset = cfg->getIntOption("username_shadow_xoffset");
  631. int shadowYOffset = cfg->getIntOption("username_shadow_yoffset");
  632. password_x = Cfg::absolutepos(cfgX, image->Width(), extents.width);
  633. password_y = Cfg::absolutepos(cfgY, image->Height(), extents.height);
  634. if (password_x >= 0 && password_y >= 0){
  635. SlimDrawString8 (draw, &entercolor, enterfont, password_x, password_y,
  636. msg, &entershadowcolor, shadowXOffset, shadowYOffset);
  637. }
  638. }
  639. if (!singleInputMode|| field == Get_Name) {
  640. msg = cfg->getOption("username_msg");
  641. XftTextExtents8(Dpy, enterfont, (XftChar8*)msg.c_str(),
  642. strlen(msg.c_str()), &extents);
  643. cfgX = cfg->getOption("username_x");
  644. cfgY = cfg->getOption("username_y");
  645. int shadowXOffset = cfg->getIntOption("username_shadow_xoffset");
  646. int shadowYOffset = cfg->getIntOption("username_shadow_yoffset");
  647. username_x = Cfg::absolutepos(cfgX, image->Width(), extents.width);
  648. username_y = Cfg::absolutepos(cfgY, image->Height(), extents.height);
  649. if (username_x >= 0 && username_y >= 0){
  650. SlimDrawString8 (draw, &entercolor, enterfont, username_x, username_y,
  651. msg, &entershadowcolor, shadowXOffset, shadowYOffset);
  652. }
  653. }
  654. XftDrawDestroy(draw);
  655. if (mode == Mode_Lock) {
  656. // If only the password box is visible, draw the user name somewhere too
  657. string user_msg = "User: " + GetName();
  658. int show_username = cfg->getIntOption("show_username");
  659. if (singleInputMode && show_username) {
  660. Message(user_msg);
  661. }
  662. }
  663. }
  664. string Panel::getSession() {
  665. return session_exec;
  666. }
  667. /* choose next available session type */
  668. void Panel::SwitchSession() {
  669. pair<string,string> ses = cfg->nextSession();
  670. session_name = ses.first;
  671. session_exec = ses.second;
  672. if (session_name.size() > 0) {
  673. ShowSession();
  674. }
  675. }
  676. /* Display session type on the screen */
  677. void Panel::ShowSession() {
  678. string msg_x, msg_y;
  679. XClearWindow(Dpy, Root);
  680. string currsession = cfg->getOption("session_msg") + " " + session_name;
  681. XGlyphInfo extents;
  682. sessionfont = XftFontOpenName(Dpy, Scr, cfg->getOption("session_font").c_str());
  683. XftDraw *draw = XftDrawCreate(Dpy, Root,
  684. DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr));
  685. XftTextExtents8(Dpy, sessionfont, reinterpret_cast<const XftChar8*>(currsession.c_str()),
  686. currsession.length(), &extents);
  687. msg_x = cfg->getOption("session_x");
  688. msg_y = cfg->getOption("session_y");
  689. int x = Cfg::absolutepos(msg_x, XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), extents.width);
  690. int y = Cfg::absolutepos(msg_y, XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)), extents.height);
  691. int shadowXOffset = cfg->getIntOption("session_shadow_xoffset");
  692. int shadowYOffset = cfg->getIntOption("session_shadow_yoffset");
  693. SlimDrawString8(draw, &sessioncolor, sessionfont, x, y,
  694. currsession,
  695. &sessionshadowcolor,
  696. shadowXOffset, shadowYOffset);
  697. XFlush(Dpy);
  698. XftDrawDestroy(draw);
  699. }
  700. void Panel::SlimDrawString8(XftDraw *d, XftColor *color, XftFont *font,
  701. int x, int y, const string& str,
  702. XftColor* shadowColor,
  703. int xOffset, int yOffset)
  704. {
  705. int calc_x = 0;
  706. int calc_y = 0;
  707. if (mode == Mode_Lock) {
  708. calc_x = viewport.x;
  709. calc_y = viewport.y;
  710. }
  711. if (xOffset && yOffset) {
  712. XftDrawStringUtf8(d, shadowColor, font,
  713. x + xOffset + calc_x,
  714. y + yOffset + calc_y,
  715. reinterpret_cast<const FcChar8*>(str.c_str()),
  716. str.length());
  717. }
  718. XftDrawStringUtf8(d, color, font,
  719. x + calc_x,
  720. y + calc_y,
  721. reinterpret_cast<const FcChar8*>(str.c_str()),
  722. str.length());
  723. }
  724. Panel::ActionType Panel::getAction(void) const{
  725. return action;
  726. }
  727. void Panel::Reset(void){
  728. ResetName();
  729. ResetPasswd();
  730. }
  731. void Panel::ResetName(void){
  732. NameBuffer.clear();
  733. }
  734. void Panel::ResetPasswd(void){
  735. PasswdBuffer.clear();
  736. HiddenPasswdBuffer.clear();
  737. }
  738. void Panel::SetName(const string& name){
  739. NameBuffer=name;
  740. if (mode == Mode_DM)
  741. action = Login;
  742. else
  743. action = Lock;
  744. }
  745. const string& Panel::GetName(void) const{
  746. return NameBuffer;
  747. }
  748. const string& Panel::GetPasswd(void) const{
  749. return PasswdBuffer;
  750. }
  751. Rectangle Panel::GetPrimaryViewport() {
  752. Rectangle fallback;
  753. Rectangle result;
  754. RROutput primary;
  755. XRROutputInfo *primary_info;
  756. XRRScreenResources *resources;
  757. XRRCrtcInfo *crtc_info;
  758. int crtc;
  759. fallback.x = 0;
  760. fallback.y = 0;
  761. fallback.width = DisplayWidth(Dpy, Scr);
  762. fallback.height = DisplayHeight(Dpy, Scr);
  763. primary = XRRGetOutputPrimary(Dpy, Win);
  764. if (!primary) {
  765. return fallback;
  766. }
  767. resources = XRRGetScreenResources(Dpy, Win);
  768. if (!resources)
  769. return fallback;
  770. primary_info = XRRGetOutputInfo(Dpy, resources, primary);
  771. if (!primary_info) {
  772. XRRFreeScreenResources(resources);
  773. return fallback;
  774. }
  775. // Fixes bug with multiple monitors. Just pick first monitor if
  776. // XRRGetOutputInfo gives returns bad into for crtc.
  777. if (primary_info->crtc < 1) {
  778. if (primary_info->ncrtc > 0) {
  779. crtc = primary_info->crtcs[0];
  780. } else {
  781. cerr << "Cannot get crtc from xrandr.\n";
  782. exit(EXIT_FAILURE);
  783. }
  784. } else {
  785. crtc = primary_info->crtc;
  786. }
  787. crtc_info = XRRGetCrtcInfo(Dpy, resources, crtc);
  788. if (!crtc_info) {
  789. XRRFreeOutputInfo(primary_info);
  790. XRRFreeScreenResources(resources);
  791. return fallback;
  792. }
  793. result.x = crtc_info->x;
  794. result.y = crtc_info->y;
  795. result.width = crtc_info->width;
  796. result.height = crtc_info->height;
  797. XRRFreeCrtcInfo(crtc_info);
  798. XRRFreeOutputInfo(primary_info);
  799. XRRFreeScreenResources(resources);
  800. return result;
  801. }
  802. void Panel::ApplyBackground(Rectangle rect) {
  803. int ret = 0;
  804. if (rect.is_empty()) {
  805. rect.x = 0;
  806. rect.y = 0;
  807. rect.width = viewport.width;
  808. rect.height = viewport.height;
  809. }
  810. ret = XCopyArea(Dpy, PanelPixmap, Win, WinGC,
  811. rect.x, rect.y, rect.width, rect.height,
  812. viewport.x + rect.x, viewport.y + rect.y);
  813. if (!ret)
  814. cerr << APPNAME << ": failed to put pixmap on the screen\n.";
  815. }