+static unsigned int
+#if USE_CARBON_EVENTS
+mac_to_emacs_modifiers (UInt32 mods)
+#else
+mac_to_emacs_modifiers (EventModifiers mods)
+#endif
+{
+ unsigned int result = 0;
+ if (mods & macShiftKey)
+ result |= shift_modifier;
+ if (mods & macCtrlKey)
+ result |= ctrl_modifier;
+ if (mods & macMetaKey)
+ result |= meta_modifier;
+ if (NILP (Vmac_command_key_is_meta) && (mods & macAltKey))
+ result |= alt_modifier;
+ return result;
+}
+
+#if USE_CARBON_EVENTS
+/* Obtains the event modifiers from the event ref and then calls
+ mac_to_emacs_modifiers. */
+static int
+mac_event_to_emacs_modifiers (EventRef eventRef)
+{
+ UInt32 mods = 0;
+ GetEventParameter (eventRef, kEventParamKeyModifiers, typeUInt32, NULL,
+ sizeof (UInt32), NULL, &mods);
+ return mac_to_emacs_modifiers (mods);
+}
+
+/* Given an event ref, return the code to use for the mouse button
+ code in the emacs input_event. */
+static int
+mac_get_mouse_btn (EventRef ref)
+{
+ EventMouseButton result = kEventMouseButtonPrimary;
+ GetEventParameter (ref, kEventParamMouseButton, typeMouseButton, NULL,
+ sizeof (EventMouseButton), NULL, &result);
+ switch (result)
+ {
+ case kEventMouseButtonPrimary:
+ return 0;
+ case kEventMouseButtonSecondary:
+ return NILP (Vmac_wheel_button_is_mouse_2) ? 1 : 2;
+ case kEventMouseButtonTertiary:
+ case 4: /* 4 is the number for the mouse wheel button */
+ return NILP (Vmac_wheel_button_is_mouse_2) ? 2 : 1;
+ default:
+ return 0;
+ }
+}
+
+/* Normally, ConvertEventRefToEventRecord will correctly handle all
+ events. However the click of the mouse wheel is not converted to a
+ mouseDown or mouseUp event. This calls ConvertEventRef, but then
+ checks to see if it is a mouse up or down carbon event that has not
+ been converted, and if so, converts it by hand (to be picked up in
+ the XTread_socket loop). */
+static Boolean mac_convert_event_ref (EventRef eventRef, EventRecord *eventRec)
+{
+ Boolean result = ConvertEventRefToEventRecord (eventRef, eventRec);
+ /* Do special case for mouse wheel button. */
+ if (!result && GetEventClass (eventRef) == kEventClassMouse)
+ {
+ UInt32 kind = GetEventKind (eventRef);
+ if (kind == kEventMouseDown && !(eventRec->what == mouseDown))
+ {
+ eventRec->what = mouseDown;
+ result=1;
+ }
+ if (kind == kEventMouseUp && !(eventRec->what == mouseUp))
+ {
+ eventRec->what = mouseUp;
+ result=1;
+ }
+ if (result)
+ {
+ /* Need where and when. */
+ UInt32 mods;
+ GetEventParameter (eventRef, kEventParamMouseLocation,
+ typeQDPoint, NULL, sizeof (Point),
+ NULL, &eventRec->where);
+ /* Use two step process because new event modifiers are
+ 32-bit and old are 16-bit. Currently, only loss is
+ NumLock & Fn. */
+ GetEventParameter (eventRef, kEventParamKeyModifiers,
+ typeUInt32, NULL, sizeof (UInt32),
+ NULL, &mods);
+ eventRec->modifiers = mods;
+
+ eventRec->when = EventTimeToTicks (GetEventTime (eventRef));
+ }
+ }
+ return result;
+}
+
+#endif