xaizek / pms (License: GPLv3+) (since 2018-12-07)
Older version of Practical Music Search written in C++.
<root> / src / input.cpp (76acf3a67d854b319deea05d1357d640c77dd9d2) (14KiB) (mode 100644) [raw]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
/* vi:set ts=8 sts=8 sw=8 noet:
 *
 * PMS  <<Practical Music Search>>
 * Copyright (C) 2006-2015  Kim Tore Jensen
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


#include "input.h"
#include "config.h"
#include "pms.h"

extern Pms *		pms;


Input::Input()
{
	this->_mode = INPUT_NORMAL;
	pending = PEND_NONE;
	text.clear();
	searchterm.clear();
	cmdhistory.clear();
	searchhistory.clear();

	winclear();
}

Input::~Input()
{
}

/*
 * Store values when an extra window parameter is needed
 */
void			Input::winstore(pms_window * w)
{
	win = w;
	winparam = param;
	winpend = pending;
	pms->log(MSG_DEBUG, 0, "winstore: win=%p, winparam=%s, winpend=%d\n", win, winparam.c_str(), winpend);
}

/*
 * Delete window parameters
 */
void			Input::winclear()
{
	win = NULL;
	winparam = "";
	winpend = PEND_NONE;
}

/*
 * Restore window parameters
 */
bool			Input::winpop()
{
	if (win == NULL) return false;
	pms->log(MSG_DEBUG, 0, "winpop: setting param=%s, pending=%d\n", param.c_str(), pending);

	param = winparam;
	pending = winpend;

	return true;
}

/*
 * Go to next history item
 */
bool			Input::gonext()
{
	vector<string>::const_iterator	e;

	if (_mode == INPUT_JUMP || _mode == INPUT_FILTER)
		e = searchhistory.end();
	else if (_mode == INPUT_COMMAND)
		e = cmdhistory.end();
	else
		return false;

	if (historypos == e)
		return false;

	++historypos;

	if (historypos == e)
	{
		text.clear();
		return false;
	}
	text = *historypos;
	return true;
}

/*
 * Go to previous history item
 */
bool			Input::goprev()
{
	vector<string>::const_iterator	e;

	if (_mode == INPUT_JUMP || _mode == INPUT_FILTER)
		e = searchhistory.begin();
	else if (_mode == INPUT_COMMAND)
		e = cmdhistory.begin();
	else
		return false;

	if (historypos == e)
		return false;

	--historypos;
	text = *historypos;
	return true;
}

/*
 * Set input mode
 */
void			Input::mode(Input_mode m)
{
	pms->log(MSG_DEBUG, 0, "Entering input mode %d\n", m);

	switch(m)
	{
		case INPUT_COMMAND:
			text.clear();
			historypos = cmdhistory.end();
			break;

		case INPUT_JUMP:
		case INPUT_FILTER:
			text.clear();
			searchterm.clear();
			historypos = searchhistory.end();
			break;

		default:
			break;
	}

	this->_mode = m;
}

Input_mode		Input::mode()
{
	return this->_mode;
}

/**
 * Read a single keystroke from ncurses. Will block until a keystroke has been registered.
 *
 * Returns the key pressed.
 */
wchar_t
Input::get_keystroke()
{
	ch = getch();
	return ch;
}

pms_pending_keys	Input::dispatch()
{
	this->pending = PEND_NONE;
	this->param.clear();

	if (_mode == INPUT_NORMAL)
		return dispatch_normal();
	else if (_mode == INPUT_LIST)
		return dispatch_list();
	else
		return dispatch_text();

	return PEND_NONE;
}

pms_pending_keys	Input::dispatch_text()
{
	switch(ch)
	{
		case KEY_RESIZE:	/* Window was resized */
			pending = PEND_RESIZE;
			break;
		case 10:		/* Return */
		case 343:		/* Enter (keypad) */
			pending = PEND_TEXT_RETURN;
			break;
		case 21:		/* ^U */
			if (text.size() == 0)
			{
				text.clear();
				pending = PEND_TEXT_ESCAPE;
			}
			else
			{
				text.clear();
				pending = PEND_TEXT_UPDATED;
			}
			break;
		case 27:		/* Escape */
			pending = PEND_RETURN_ESCAPE;
			break;
		case 8:			/* ^H -- backspace */
		case 127:		/* ^? -- delete */
		case KEY_BACKSPACE:
			if (text.size() > 0)
			{
				text.erase(--string::iterator(text.end()));
				pending = PEND_TEXT_UPDATED;
			}
			else
			{
				pending = PEND_TEXT_ESCAPE;
			}
			break;
		case KEY_MOUSE:
			pending = PEND_NONE;
			break;
		default:
			if (ch < 32 || ch >= KEY_CODE_YES)
			{
				pending = pms->bindings->act(ch, &param);
				if (pending != PEND_NONE)
				{
					break;
				}
				pms->log(MSG_DEBUG, 0, "Key %3d '%c' pressed in text mode but not textual and not bound.\n", ch, ch);
			}
			text += ch;

			pending = PEND_TEXT_UPDATED;
	}

	return pending;
}

pms_pending_keys	Input::dispatch_list()
{
	if (ch == 10 || ch == 343)
	{
		pending = PEND_RETURN;
		return pending;
	}
	else if (ch == 27)
	{
		pending = PEND_RETURN_ESCAPE;
		return pending;
	}

	return dispatch_normal();
}

pms_pending_keys	Input::dispatch_normal()
{
	MEVENT			mouseevent;
	int			mousewinx, mousewiny;
	bool			mousecurwin = false;
	bool			mousetopbar = false;
	bool			mousestatusbar = false;
	bool			mousepositionreadout = false;
	bool			mousemodshift = false;
	bool			mousemodctrl = false;
	bool			mousemodalt = false;
	int			mouselistindex;

	if (ch == -1)
		return PEND_NONE;

	if (ch == KEY_RESIZE)
	{
		pending = PEND_RESIZE;
		return pending;
	}

	/* Mouse event */
	if (ch == KEY_MOUSE)
	{
		if (getmouse(&mouseevent) == ERR)
		{
			pms->log(MSG_DEBUG, 0, "error with getmouse()\n");
			ch = -1; // prevents weird results
			return PEND_NONE;
		}

		pms->log(MSG_DEBUG, 0, "mevent x:%d, y:%d, z:%d\n", mouseevent.x, mouseevent.y, mouseevent.z);

		if (mouseevent.bstate & BUTTON_SHIFT)
		{
			pms->log(MSG_DEBUG, 0, "shift is down\n");
			mousemodshift = true;
		}
		if (mouseevent.bstate & BUTTON_CTRL)
		{
			pms->log(MSG_DEBUG, 0, "ctrl is down\n");
			mousemodctrl = true;
		}
		if (mouseevent.bstate & BUTTON_ALT)
		{
			pms->log(MSG_DEBUG, 0, "alt is down\n");
			mousemodctrl = true;
		}

		mousewinx = mouseevent.x;
		mousewiny = mouseevent.y;

		if (pms->disp->actwin() && wenclose(pms->disp->actwin()->h(), mouseevent.y, mouseevent.x))
		{
			pms->log(MSG_DEBUG, 0, "mouse event in current window\n");
			mousecurwin = true;
			wmouse_trafo(pms->disp->actwin()->h(), &mousewiny, &mousewinx, false);

			//take window title and column titles away
			mousewiny -= 2;

			mouselistindex = pms->disp->actwin()->cursordrawstart() + mousewiny;
			if (!pms->disp->actwin()->plist() || mouselistindex >= pms->disp->actwin()->plist()->size())
			{
				//not a playlist or clicked off the end of the 
				//list
				mouselistindex = -1;
				pms->log(MSG_DEBUG, 0, "mouse event off the end of the list of songs or not a playlist\n");
			}
		}
		else if (wenclose(pms->disp->topbar->h(), mouseevent.y, mouseevent.x))
		{
			pms->log(MSG_DEBUG, 0, "mouse event in topbar\n");
			mousetopbar = true;
			wmouse_trafo(pms->disp->topbar->h(), &mousewiny, &mousewinx, false);
		}
		else if (wenclose(pms->disp->statusbar->h(), mouseevent.y, mouseevent.x))
		{
			pms->log(MSG_DEBUG, 0, "mouse event in statusbar\n");
			mousestatusbar = true;
			wmouse_trafo(pms->disp->statusbar->h(), &mousewiny, &mousewinx, false);
		}
		else if (wenclose(pms->disp->positionreadout->h(), mouseevent.y, mouseevent.x))
		{
			pms->log(MSG_DEBUG, 0, "mouse event in positionreadout\n");
			mousepositionreadout = true;
			wmouse_trafo(pms->disp->positionreadout->h(), &mousewiny, &mousewinx, false);
		}
		else
		{
			pms->log(MSG_DEBUG, 0, "mouse event doesn't seem to be enclosed in any of our windows\n");
			return PEND_NONE;
		}

		pms->log(MSG_DEBUG, 0, "mouse event at row %d, col %d of window\n", mousewiny, mousewinx);

		if (mouseevent.bstate & MOUSEWHEEL_DOWN)
		{
			pms->log(MSG_DEBUG, 0, "mousewheel down\n");
			if (mousetopbar)
			{
				if (mousemodctrl)
				{
					param = "-3";
					return PEND_VOLUME;
				}
				return PEND_NEXT;
			}
			if (mousecurwin)
			{
				if (mousewiny == -2) //heading bar
					return PEND_NEXTWIN;
				return PEND_SCROLL_DOWN;
			}
			return PEND_NONE;
		}
		else if (mouseevent.bstate & MOUSEWHEEL_UP)
		{
			pms->log(MSG_DEBUG, 0, "mousewheel up\n");
			if (mousetopbar)
			{
				if (mousemodctrl) {
					param = "+3";
					return PEND_VOLUME;
				}
				return PEND_PREV;
			}
			if (mousecurwin)
			{
				if (mousewiny == -2) //heading bar
					return PEND_PREVWIN;
				return PEND_SCROLL_UP;
			}
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON1_PRESSED)
		{
			pms->log(MSG_DEBUG, 0, "button 1 down\n");
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON1_RELEASED)
		{
			pms->log(MSG_DEBUG, 0, "button 1 released\n");
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON1_CLICKED)
		{
			pms->log(MSG_DEBUG, 0, "button 1 clicked\n");
			if (mousetopbar)
				return PEND_TOGGLEPLAY;
			if (mousecurwin)
			{
				if (mousewiny == -2) //heading bar
					return PEND_NEXTWIN;
				if (mouselistindex >= 0) //song
				{
					pms->disp->actwin()->plist()->setcursor(mouselistindex);
					if (mousemodctrl)
						pms->disp->actwin()->plist()->selectsong(pms->disp->actwin()->plist()->song(mouselistindex), !pms->disp->actwin()->plist()->song(mouselistindex)->selected);
					return PEND_NONE;
				}
			}
			if (mousestatusbar)
				return PEND_COMMANDMODE;
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON1_DOUBLE_CLICKED)
		{
			pms->log(MSG_DEBUG, 0, "button 1 doubleclicked\n");
			if (mousetopbar)
				return PEND_STOP;
			if (mousecurwin)
			{
				if (mousewiny == -2) //heading bar
					return PEND_PREVWIN;
				if (mouselistindex >= 0) //song
				{
					pms->disp->actwin()->plist()->setcursor(mouselistindex);
					return PEND_PLAY;
				}
			}
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON1_TRIPLE_CLICKED)
		{
			pms->log(MSG_DEBUG, 0, "button 1 tripleclicked\n");
			if (mousecurwin && mouselistindex >= 0)
			{
				pms->disp->actwin()->plist()->setcursor(mouselistindex);
				return PEND_ADD;
			}
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON2_PRESSED)
		{
			pms->log(MSG_DEBUG, 0, "button 2 down\n");
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON2_RELEASED)
		{
			pms->log(MSG_DEBUG, 0, "button 2 released\n");
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON2_CLICKED)
		{
			pms->log(MSG_DEBUG, 0, "button 2 clicked\n");
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON2_DOUBLE_CLICKED)
		{
			pms->log(MSG_DEBUG, 0, "button 2 doubleclicked\n");
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON2_TRIPLE_CLICKED)
		{
			pms->log(MSG_DEBUG, 0, "button 2 tripleclicked\n");
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON3_PRESSED)
		{
			pms->log(MSG_DEBUG, 0, "button 3 down\n");
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON3_RELEASED)
		{
			pms->log(MSG_DEBUG, 0, "button 3 released\n");
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON3_CLICKED)
		{
			pms->log(MSG_DEBUG, 0, "button 3 clicked\n");
			if (mousecurwin)
			{
				if (mouselistindex >= 0) //song
				{
					pms->disp->actwin()->plist()->setcursor(mouselistindex);
					pms->disp->actwin()->plist()->selectsong(pms->disp->actwin()->plist()->song(mouselistindex), !pms->disp->actwin()->plist()->song(mouselistindex)->selected);
					return PEND_NONE;
				}
			}
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON3_DOUBLE_CLICKED)
		{
			pms->log(MSG_DEBUG, 0, "button 3 doubleclicked\n");
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON3_TRIPLE_CLICKED)
		{
			pms->log(MSG_DEBUG, 0, "button 3 tripleclicked\n");
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON4_PRESSED)
		{
			pms->log(MSG_DEBUG, 0, "button 4 down\n");
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON4_RELEASED)
		{
			pms->log(MSG_DEBUG, 0, "button 4 released\n");
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON4_CLICKED)
		{
			pms->log(MSG_DEBUG, 0, "button 4 clicked\n");
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON4_DOUBLE_CLICKED)
		{
			pms->log(MSG_DEBUG, 0, "button 4 doubleclicked\n");
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON4_TRIPLE_CLICKED)
		{
			pms->log(MSG_DEBUG, 0, "button 4 tripleclicked\n");
			return PEND_NONE;
		}
#if NCURSES_MOUSE_VERSION > 1
		else if (mouseevent.bstate & BUTTON5_PRESSED)
		{
			pms->log(MSG_DEBUG, 0, "button 5 down\n");
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON5_RELEASED)
		{
			pms->log(MSG_DEBUG, 0, "button 5 released\n");
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON5_CLICKED)
		{
			pms->log(MSG_DEBUG, 0, "button 5 clicked\n");
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON5_DOUBLE_CLICKED)
		{
			pms->log(MSG_DEBUG, 0, "button 5 doubleclicked\n");
			return PEND_NONE;
		}
		else if (mouseevent.bstate & BUTTON5_TRIPLE_CLICKED)
		{
			pms->log(MSG_DEBUG, 0, "button 5 tripleclicked\n");
			return PEND_NONE;
		}
#endif
		else if (mouseevent.bstate & REPORT_MOUSE_POSITION)
		{
			pms->log(MSG_DEBUG, 0, "mouse position -- what does this do?\n");
			return PEND_NONE;
		}
		else
		{
			pms->log(MSG_DEBUG, 0, "mevent state (%d) unknown\n", mouseevent.bstate);
			return PEND_NONE;
		}
	}

	/* Key pressed */
	pending = pms->bindings->act(ch, &param);

	if (pending == PEND_NONE)
	{
		pms->log(MSG_STATUS, STERR, _("Key is not bound."));
		pms->log(MSG_DEBUG, 0, "Key %3d '%c' pressed but not bound.\n", ch, ch);
	}

	return pending;
}

/*
 * Saves the current text in history
 */
void		Input::savehistory()
{
	switch(_mode)
	{
		case INPUT_JUMP:
		case INPUT_FILTER:
			searchhistory.push_back(text);
			historypos = searchhistory.end();
			break;

		case INPUT_COMMAND:
			cmdhistory.push_back(text);
			historypos = cmdhistory.end();
			break;

		default:
			break;
	}
}

/*
 * Run a command
 */
bool		Input::run(string s, Message & err)
{
	int		pos;

	if (s.size() == 0)
		return true;

	pos = s.find_first_of(" ");
	if (pos > 0)
	{
		param = s.substr(pos + 1);
		s = s.substr(0, pos);
		pms->log(MSG_DEBUG, 0, "Running command '%s' with param '%s'\n", s.c_str(), param.c_str());
	}
	else
		pms->log(MSG_DEBUG, 0, "Running command '%s' without parameters\n", s.c_str());

	pending = pms->commands->act(s);

	if (pending == PEND_NONE)
	{
		err.code = CERR_UNKNOWN_COMMAND;
		err.str = _("unknown command");
		err.str += " '" + s + "'";
		return false;
	}

	return true;
}
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/pms

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/pms

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master