//////////////////////////  接口方法  //////////////////////////
//添加时钟
bool EventServerEpoll::AddTimer(IEventHandler *handler, uint32_t timeout_ms, bool persist)
{
	assert(handler != NULL);
	EventInfo *event_info = new EventInfo;
	EventType type = persist?ET_PERSIST:ET_EMPTY;

	SetEventInfo(event_info, -1, type, handler, timeout_ms);
	SetTimerInfo(event_info, timeout_ms);
	if(m_TimerHeap.Insert((HeapItem*)event_info))
		return true;

	//failed
	LOG_ERROR(logger, "add timer failed.");
	delete event_info;
	return false;
}
示例#2
0
bool CEventInfoPopup::Show(const CEventInfoData *pEventInfo,const RECT *pPos)
{
	if (pEventInfo==NULL)
		return false;
	bool fExists=m_hwnd!=NULL;
	if (!fExists) {
		if (!Create(NULL,WS_POPUP | WS_CLIPCHILDREN | WS_THICKFRAME,WS_EX_TOPMOST | WS_EX_NOACTIVATE,0))
			return false;
	}
	if (pPos!=NULL) {
		if (!GetVisible())
			SetPosition(pPos);
	} else if (!IsVisible() || m_EventInfo!=*pEventInfo) {
		RECT rc;
		POINT pt;
		int Width,Height;

		GetPosition(&rc);
		Width=rc.right-rc.left;
		Height=rc.bottom-rc.top;
		::GetCursorPos(&pt);
		pt.y+=16;
		HMONITOR hMonitor=::MonitorFromPoint(pt,MONITOR_DEFAULTTONEAREST);
		if (hMonitor!=NULL) {
			MONITORINFO mi;

			mi.cbSize=sizeof(mi);
			if (::GetMonitorInfo(hMonitor,&mi)) {
				if (pt.x+Width>mi.rcMonitor.right)
					pt.x=mi.rcMonitor.right-Width;
				if (pt.y+Height>mi.rcMonitor.bottom) {
					pt.y=mi.rcMonitor.bottom-Height;
					if (pt.x+Width<mi.rcMonitor.right)
						pt.x+=min(16,mi.rcMonitor.right-(pt.x+Width));
				}
			}
		}
		::SetWindowPos(m_hwnd,HWND_TOPMOST,pt.x,pt.y,Width,Height,
					   SWP_NOACTIVATE);
	}
	SetEventInfo(pEventInfo);
	::ShowWindow(m_hwnd,SW_SHOWNA);
	return true;
}
示例#3
0
文件: extinit.c 项目: nikai3d/xserver
static void
FixExtensionEvents(ExtensionEntry * extEntry)
{
    DeviceValuator = extEntry->eventBase;
    DeviceKeyPress = DeviceValuator + 1;
    DeviceKeyRelease = DeviceKeyPress + 1;
    DeviceButtonPress = DeviceKeyRelease + 1;
    DeviceButtonRelease = DeviceButtonPress + 1;
    DeviceMotionNotify = DeviceButtonRelease + 1;
    DeviceFocusIn = DeviceMotionNotify + 1;
    DeviceFocusOut = DeviceFocusIn + 1;
    ProximityIn = DeviceFocusOut + 1;
    ProximityOut = ProximityIn + 1;
    DeviceStateNotify = ProximityOut + 1;
    DeviceMappingNotify = DeviceStateNotify + 1;
    ChangeDeviceNotify = DeviceMappingNotify + 1;
    DeviceKeyStateNotify = ChangeDeviceNotify + 1;
    DeviceButtonStateNotify = DeviceKeyStateNotify + 1;
    DevicePresenceNotify = DeviceButtonStateNotify + 1;
    DevicePropertyNotify = DevicePresenceNotify + 1;

    event_base[KeyClass] = DeviceKeyPress;
    event_base[ButtonClass] = DeviceButtonPress;
    event_base[ValuatorClass] = DeviceMotionNotify;
    event_base[ProximityClass] = ProximityIn;
    event_base[FocusClass] = DeviceFocusIn;
    event_base[OtherClass] = DeviceStateNotify;

    BadDevice += extEntry->errorBase;
    BadEvent += extEntry->errorBase;
    BadMode += extEntry->errorBase;
    DeviceBusy += extEntry->errorBase;
    BadClass += extEntry->errorBase;

    SetMaskForExtEvent(DeviceKeyPressMask, DeviceKeyPress);
    AllowPropagateSuppress(DeviceKeyPressMask);
    SetCriticalEvent(DeviceKeyPress);

    SetMaskForExtEvent(DeviceKeyReleaseMask, DeviceKeyRelease);
    AllowPropagateSuppress(DeviceKeyReleaseMask);
    SetCriticalEvent(DeviceKeyRelease);

    SetMaskForExtEvent(DeviceButtonPressMask, DeviceButtonPress);
    AllowPropagateSuppress(DeviceButtonPressMask);
    SetCriticalEvent(DeviceButtonPress);

    SetMaskForExtEvent(DeviceButtonReleaseMask, DeviceButtonRelease);
    AllowPropagateSuppress(DeviceButtonReleaseMask);
    SetCriticalEvent(DeviceButtonRelease);

    SetMaskForExtEvent(DeviceProximityMask, ProximityIn);
    SetMaskForExtEvent(DeviceProximityMask, ProximityOut);

    SetMaskForExtEvent(DeviceStateNotifyMask, DeviceStateNotify);

    SetMaskForExtEvent(DevicePointerMotionMask, DeviceMotionNotify);
    AllowPropagateSuppress(DevicePointerMotionMask);
    SetCriticalEvent(DeviceMotionNotify);

    SetEventInfo(DevicePointerMotionHintMask, _devicePointerMotionHint);
    SetEventInfo(DeviceButton1MotionMask, _deviceButton1Motion);
    SetEventInfo(DeviceButton2MotionMask, _deviceButton2Motion);
    SetEventInfo(DeviceButton3MotionMask, _deviceButton3Motion);
    SetEventInfo(DeviceButton4MotionMask, _deviceButton4Motion);
    SetEventInfo(DeviceButton5MotionMask, _deviceButton5Motion);
    SetEventInfo(DeviceButtonMotionMask, _deviceButtonMotion);

    SetMaskForExtEvent(DeviceFocusChangeMask, DeviceFocusIn);
    SetMaskForExtEvent(DeviceFocusChangeMask, DeviceFocusOut);

    SetMaskForExtEvent(DeviceMappingNotifyMask, DeviceMappingNotify);
    SetMaskForExtEvent(ChangeDeviceNotifyMask, ChangeDeviceNotify);

    SetEventInfo(DeviceButtonGrabMask, _deviceButtonGrab);
    SetExclusiveAccess(DeviceButtonGrabMask);

    SetEventInfo(DeviceOwnerGrabButtonMask, _deviceOwnerGrabButton);
    SetEventInfo(DevicePresenceNotifyMask, _devicePresence);
    SetMaskForExtEvent(DevicePropertyNotifyMask, DevicePropertyNotify);

    SetEventInfo(0, _noExtensionEvent);
}
//设置IO事件
bool EventServerEpoll::SetEvent(int32_t fd, EventType type, IEventHandler *handler, int32_t timeout_ms)
{
	if(fd<0 || ET_IS_EMPTY(type) || handler==NULL)
	{
		LOG_WARN(logger, "add_event but parameters invalid. fd="<<fd<<",type="<<type<<"("<<EventStr(type)<<").");
		return false;
	}

	FDMap::iterator it = m_FDMap.find(fd);
    //新增事件
	if(it == m_FDMap.end())
	{
		EventInfo *event_info = (EventInfo *)m_ObjectPool.Get();
		if(event_info == NULL)
		{
			LOG_ERROR(logger, "out of memory. fd="<<fd<<",type="<<type<<"("<<EventStr(type)<<").");
			return false;
		}
		SetEventInfo(event_info, fd, type, handler, timeout_ms);

		//保存到event map
		std::pair<FDMap::iterator, bool> result = m_FDMap.insert(std::make_pair(fd, (void*)event_info));
		if(result.second == false)
		{
			LOG_ERROR(logger, "insert event_map failed. fd="<<fd<<",type="<<type<<"("<<EventStr(type)<<").");
			m_ObjectPool.Recycle((void*)event_info);
			return false;
		}

		//添加时钟
		if(timeout_ms >= 0)
		{
			SetTimerInfo(event_info, timeout_ms);
			if(!m_TimerHeap.Insert((HeapItem*)event_info))
			{
				LOG_ERROR(logger, "add timer failed. fd="<<fd<<",type="<<type<<"("<<EventStr(type)<<").");
				m_FDMap.erase(fd);
				m_ObjectPool.Recycle((void*)event_info);
				return false;
			}
		}

		//添加事件
		struct epoll_event ep_event;
		ep_event.events = 0;
		ep_event.data.ptr = (void*)event_info;
		if(ET_IS_READ(type))
			ep_event.events |= EPOLLIN;
		if(ET_IS_WRITE(type))
			ep_event.events |= EPOLLOUT;
		if(epoll_ctl(m_EpFd, EPOLL_CTL_ADD, fd, &ep_event) != 0)
		{
			LOG_ERROR(logger, "add event failed. fd="<<fd<<",type="<<type<<"("<<EventStr(type)<<")"<<",errno="<<errno<<"("<<strerror(errno)<<").");
			m_FDMap.erase(fd);
			if(timeout_ms >= 0)
				m_TimerHeap.Remove((HeapItem*)event_info);
			m_ObjectPool.Recycle((void*)event_info);
			return false;
		}

		return true;
	}

	//已经存在,修改事件
	EventInfo *event_info = (EventInfo *)it->second;
	EventType old_type = event_info->type&ET_RDWT;
	EventType new_type = (event_info->type|type)&ET_RDWT;
	if(new_type == old_type)         //已经存在
	{
		LOG_DEBUG(logger, "new event_type is equal to the old. nothing to do.fd="<<fd);
		event_info->type |= type;    //maybe EV_PERSIST
		return true;
	}
	new_type = event_info->type|type;

	struct epoll_event ep_event;
	ep_event.events = 0;
	ep_event.data.ptr = (void*)event_info;
	if(ET_IS_READ(new_type))
		ep_event.events |= EPOLLIN;
	if(ET_IS_WRITE(new_type))
		ep_event.events |= EPOLLOUT;
	if(epoll_ctl(m_EpFd, EPOLL_CTL_MOD, fd, &ep_event) != 0)
	{
		LOG_ERROR(logger, "modify event failed. fd="<<fd<<",old_type="<<event_info->type<<"("<<EventStr(event_info->type)<<")."<<",new_type="<<new_type<<"("<<EventStr(new_type)<<")"<<",errno="<<errno<<"("<<strerror(errno)<<").");
		return false;
	}

	event_info->type = new_type;
	return true;
}