Exemplo n.º 1
0
static void _gfx_x11_event_proc(

		XEvent* event)
{
	/* Get window */
	GFX_PlatformWindow window = GFX_UINT_TO_VOID(event->xany.window);

	switch(event->type)
	{
		/* Protocol messages */
		case ClientMessage :
		{
			if(event->xclient.data.l[0] == _gfx_x11.WM_DELETE_WINDOW)
				_gfx_event_window_close(window);

			break;
		}

		/* Resize & Move */
		case ConfigureNotify :
		{
			GFX_X11_Window* internal =
				_gfx_x11_get_window_from_handle(event->xany.window);

			if(!internal) break;

			if(
				internal->x != event->xconfigure.x ||
				internal->y != event->xconfigure.y)
			{
				internal->x = event->xconfigure.x;
				internal->y = event->xconfigure.y;

				int xS = 0;
				int yS = 0;

				GFX_X11_Monitor* monitor =
					_gfx_platform_window_get_monitor(window);

				if(monitor)
				{
					xS = monitor->x;
					yS = monitor->y;
				}

				_gfx_event_window_move(
					window,
					internal->x - xS,
					internal->y - yS
				);
			}

			if(
				internal->width != event->xconfigure.width ||
				internal->height != event->xconfigure.height)
			{
				internal->width = event->xconfigure.width;
				internal->height = event->xconfigure.height;

				_gfx_event_window_resize(
					window,
					internal->width,
					internal->height
				);
			}

			break;
		}

		/* Focus */
		case FocusIn :
		{
			/* Enter fullscreen */
			GFX_X11_Window* internal =
				_gfx_x11_get_window_from_handle(event->xany.window);

			if(!internal) break;

			if(
				(internal->flags & GFX_X11_FULLSCREEN) &&
				!(internal->flags & GFX_X11_HIDDEN))
			{
				_gfx_x11_enter_fullscreen(
					internal->monitor,
					event->xany.window,
					internal->mode
				);
			}

			_gfx_event_window_focus(window);

			break;
		}

		/* Blur */
		case FocusOut :
		{
			/* Leave fullscreen */
			GFX_X11_Window* internal =
				_gfx_x11_get_window_from_handle(event->xany.window);

			if(!internal) break;

			if(internal->flags & GFX_X11_FULLSCREEN)
			{
				_gfx_x11_leave_fullscreen(internal->monitor);

				if(!(internal->flags & GFX_X11_HIDDEN))
					XIconifyWindow(
						_gfx_x11.display,
						event->xany.window,
						XScreenNumberOfScreen(internal->monitor->screen)
					);
			}

			_gfx_event_window_blur(window);

			break;
		}

		/* Key press */
		case KeyPress :
		{
			GFXKey key;
			if(event->xkey.keycode > GFX_X11_MAX_KEYCODE) key = GFX_KEY_UNKNOWN;
			else key = _gfx_x11.keys[event->xkey.keycode];

			_gfx_event_key_press(
				window,
				key,
				_gfx_x11_get_key_state(event->xkey.state)
			);

			break;
		}

		/* Key release */
		case KeyRelease :
		{
			GFXKey key;
			if(event->xkey.keycode > GFX_X11_MAX_KEYCODE) key = GFX_KEY_UNKNOWN;
			else key = _gfx_x11.keys[event->xkey.keycode];

			_gfx_event_key_release(
				window,
				key,
				_gfx_x11_get_key_state(event->xkey.state)
			);

			break;
		}

		/* Pointer motion */
		case MotionNotify :
		{
			_gfx_event_mouse_move(
				window,
				event->xmotion.x,
				event->xmotion.y,
				_gfx_x11_get_key_state(event->xmotion.state)
			);

			break;
		}

		/* Pointer enter */
		case EnterNotify :
		{
			_gfx_event_mouse_enter(
				window,
				event->xcrossing.x,
				event->xcrossing.y,
				_gfx_x11_get_key_state(event->xcrossing.state)
			);

			break;
		}

		/* Pointer leave */
		case LeaveNotify :
		{
			_gfx_event_mouse_leave(
				window,
				event->xcrossing.x,
				event->xcrossing.y,
				_gfx_x11_get_key_state(event->xcrossing.state)
			);

			break;
		}

		/* Mouse key press */
		case ButtonPress :
		{
			GFXKeyState state = _gfx_x11_get_key_state(event->xbutton.state);
			int x = event->xbutton.x;
			int y = event->xbutton.y;

			switch(event->xbutton.button)
			{
				case Button1 :
					_gfx_event_mouse_press(window, GFX_MOUSE_KEY_LEFT, x, y, state);
					break;
				case Button2 :
					_gfx_event_mouse_press(window, GFX_MOUSE_KEY_MIDDLE, x, y, state);
					break;
				case Button3 :
					_gfx_event_mouse_press(window, GFX_MOUSE_KEY_RIGHT, x, y, state);
					break;

				case Button4 :
					_gfx_event_mouse_wheel(window, 0, 1, x, y, state);
					break;
				case Button5 :
					_gfx_event_mouse_wheel(window, 0, -1, x, y, state);
					break;
				case Button6 :
					_gfx_event_mouse_wheel(window, -1, 0, x, y, state);
					break;
				case Button7 :
					_gfx_event_mouse_wheel(window, 1, 0, x, y, state);
					break;
			}
		}

		/* Mouse key release */
		case ButtonRelease :
		{
			GFXKeyState state = _gfx_x11_get_key_state(event->xbutton.state);
			int x = event->xbutton.x;
			int y = event->xbutton.y;

			switch(event->xbutton.button)
			{
				case Button1 :
					_gfx_event_mouse_release(window, GFX_MOUSE_KEY_LEFT, x, y, state);
					break;
				case Button2 :
					_gfx_event_mouse_release(window, GFX_MOUSE_KEY_MIDDLE, x, y, state);
					break;
				case Button3 :
					_gfx_event_mouse_release(window, GFX_MOUSE_KEY_RIGHT, x, y, state);
					break;
			}
		}
	}
}
Exemplo n.º 2
0
static LRESULT CALLBACK _gfx_win32_window_proc(

		HWND    handle,
		UINT    msg,
		WPARAM  wParam,
		LPARAM  lParam)
{
	/* Get window */
	GFX_PlatformWindow window = (GFX_PlatformWindow)handle;

	switch(msg)
	{
		/* Close button */
		case WM_CLOSE :
		{
			_gfx_event_window_close(window);
			return 0;
		}

		/* Move */
		case WM_MOVE :
		{
			int xS = 0;
			int yS = 0;

			GFX_Win32_Monitor* monitor =
				_gfx_platform_window_get_monitor(window);

			if(monitor)
			{
				xS = monitor->x;
				yS = monitor->y;
			}

			int x = (int)(short)LOWORD(lParam);
			int y = (int)(short)HIWORD(lParam);

			_gfx_event_window_move(window, x - xS, y - yS);

			return 0;
		}

		/* Resize */
		case WM_SIZE :
		{
			_gfx_event_window_resize(
				window,
				LOWORD(lParam),
				HIWORD(lParam)
			);
			return 0;
		}

		/* Focus */
		case WM_SETFOCUS :
		{
			/* Enter fullscreen */
			GFX_Win32_Window* internal =
				_gfx_win32_get_window_from_handle(handle);

			if(!internal) return 0;

			if(
				(internal->flags & GFX_WIN32_FULLSCREEN) &&
				!(internal->flags & GFX_WIN32_HIDDEN))
			{
				_gfx_win32_enter_fullscreen(
					internal->monitor,
					internal->mode
				);
			}

			_gfx_event_window_focus(window);

			return 0;
		}

		/* Blur */
		case WM_KILLFOCUS :
		{
			/* Leave fullscreen */
			GFX_Win32_Window* internal =
				_gfx_win32_get_window_from_handle(handle);

			if(!internal) return 0;

			if(internal->flags & GFX_WIN32_FULLSCREEN)
			{
				_gfx_win32_leave_fullscreen(internal->monitor);

				if(!(internal->flags & GFX_WIN32_HIDDEN))
					ShowWindow(handle, SW_MINIMIZE);
			}

			_gfx_event_window_blur(window);

			return 0;
		}

		/* Key press */
		case WM_KEYDOWN :
		case WM_SYSKEYDOWN :
		{
			GFXKey key;
			if(wParam > GFX_WIN32_MAX_KEYCODE)
				key = GFX_KEY_UNKNOWN;
			else key = _gfx_win32_get_extended_key(
				_gfx_win32.keys[wParam],
				lParam);

			_gfx_event_key_press(window, key, _gfx_win32_get_key_state());

			return 0;
		}

		/* Key release */
		case WM_KEYUP :
		case WM_SYSKEYUP :
		{
			GFXKey key;
			if(wParam > GFX_WIN32_MAX_KEYCODE)
				key = GFX_KEY_UNKNOWN;
			else key = _gfx_win32_get_extended_key(
				_gfx_win32.keys[wParam],
				lParam);

			_gfx_event_key_release(window, key, _gfx_win32_get_key_state());

			return 0;
		}

		/* Mouse move */
		case WM_MOUSEMOVE :
		{
			int x = GET_X_LPARAM(lParam);
			int y = GET_Y_LPARAM(lParam);
			GFXKeyState state = _gfx_win32_get_key_state();

			/* Check mouse enter event */
			GFX_Win32_Window* internal =
				_gfx_win32_get_window_from_handle(handle);

			if(!internal) return 0;

			if(!(internal->flags & GFX_WIN32_MOUSEINSIDE))
			{
				internal->flags |= GFX_WIN32_MOUSEINSIDE;
				_gfx_win32_track_mouse(handle);

				_gfx_event_mouse_enter(window, x, y, state);
			}

			else _gfx_event_mouse_move(window, x, y, state);

			return 0;
		}

		/* Mouse leave */
		case WM_MOUSELEAVE :
		{
			GFX_Win32_Window* internal =
				_gfx_win32_get_window_from_handle(handle);

			if(!internal) return 0;
			internal->flags &= ~GFX_WIN32_MOUSEINSIDE;

			/* Untrack */
			TRACKMOUSEEVENT track;
			ZeroMemory(&track, sizeof(TRACKMOUSEEVENT));

			track.cbSize    = sizeof(TRACKMOUSEEVENT);
			track.dwFlags   = TME_CANCEL | TME_LEAVE;
			track.hwndTrack = handle;

			TrackMouseEvent(&track);

			/* Get mouse position */
			POINT pnt;
			ZeroMemory(&pnt, sizeof(POINT));
			GetCursorPos(&pnt);

			ScreenToClient(handle, &pnt);

			_gfx_event_mouse_leave(
				window,
				pnt.x,
				pnt.y,
				_gfx_win32_get_key_state()
			);

			return 0;
		}

		/* Left mouse button */
		case WM_LBUTTONDOWN :
		{
			_gfx_event_mouse_press(window,
				GFX_MOUSE_KEY_LEFT,
				GET_X_LPARAM(lParam),
				GET_Y_LPARAM(lParam),
				_gfx_win32_get_key_state()
			);
			return 0;
		}
		case WM_LBUTTONUP :
		{
			_gfx_event_mouse_release(window,
				GFX_MOUSE_KEY_LEFT,
				GET_X_LPARAM(lParam),
				GET_Y_LPARAM(lParam),
				_gfx_win32_get_key_state()
			);
			return 0;
		}

		/* Right mouse button */
		case WM_RBUTTONDOWN :
		{
			_gfx_event_mouse_press(window,
				GFX_MOUSE_KEY_RIGHT,
				GET_X_LPARAM(lParam),
				GET_Y_LPARAM(lParam),
				_gfx_win32_get_key_state()
			);
			return 0;
		}
		case WM_RBUTTONUP :
		{
			_gfx_event_mouse_release(window,
				GFX_MOUSE_KEY_RIGHT,
				GET_X_LPARAM(lParam),
				GET_Y_LPARAM(lParam),
				_gfx_win32_get_key_state()
			);
			return 0;
		}

		/* Middle mouse button */
		case WM_MBUTTONDOWN :
		{
			_gfx_event_mouse_press(window,
				GFX_MOUSE_KEY_MIDDLE,
				GET_X_LPARAM(lParam),
				GET_Y_LPARAM(lParam),
				_gfx_win32_get_key_state()
			);
			return 0;
		}
		case WM_MBUTTONUP :
		{
			_gfx_event_mouse_release(window,
				GFX_MOUSE_KEY_MIDDLE,
				GET_X_LPARAM(lParam),
				GET_Y_LPARAM(lParam),
				_gfx_win32_get_key_state()
			);
			return 0;
		}

		/* Vertical mouse wheel */
		case WM_MOUSEWHEEL :
		{
			_gfx_event_mouse_wheel(window,
				0, GET_WHEEL_DELTA_WPARAM(wParam),
				GET_X_LPARAM(lParam),
				GET_Y_LPARAM(lParam),
				_gfx_win32_get_key_state()
			);

			return 0;
		}

		/* Horizontal mouse wheel */
		case WM_MOUSEHWHEEL :
		{
			_gfx_event_mouse_wheel(window,
				GET_WHEEL_DELTA_WPARAM(wParam), 0,
				GET_X_LPARAM(lParam),
				GET_Y_LPARAM(lParam),
				_gfx_win32_get_key_state()
			);

			return 0;
		}
	}

	return DefWindowProc(handle, msg, wParam, lParam);
}