aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/macselect.c367
1 files changed, 324 insertions, 43 deletions
diff --git a/src/macselect.c b/src/macselect.c
index 1b6a1a46cc3..40a3db9ca08 100644
--- a/src/macselect.c
+++ b/src/macselect.c
@@ -909,14 +909,13 @@ and t is the same as `SECONDARY'. */)
909} 909}
910 910
911 911
912/***********************************************************************
913 Apple event support
914***********************************************************************/
912int mac_ready_for_apple_events = 0; 915int mac_ready_for_apple_events = 0;
913static Lisp_Object Vmac_apple_event_map; 916static Lisp_Object Vmac_apple_event_map;
914static Lisp_Object Qmac_apple_event_class, Qmac_apple_event_id; 917static Lisp_Object Qmac_apple_event_class, Qmac_apple_event_id;
915static struct 918static Lisp_Object Qemacs_suspension_id;
916{
917 AppleEvent *buf;
918 int size, count;
919} deferred_apple_events;
920extern Lisp_Object Qundefined; 919extern Lisp_Object Qundefined;
921extern void mac_store_apple_event P_ ((Lisp_Object, Lisp_Object, 920extern void mac_store_apple_event P_ ((Lisp_Object, Lisp_Object,
922 const AEDesc *)); 921 const AEDesc *));
@@ -927,6 +926,19 @@ struct apple_event_binding
927 Lisp_Object key, binding; 926 Lisp_Object key, binding;
928}; 927};
929 928
929struct suspended_ae_info
930{
931 UInt32 expiration_tick, suspension_id;
932 AppleEvent apple_event, reply;
933 struct suspended_ae_info *next;
934};
935
936/* List of deferred apple events at the startup time. */
937static struct suspended_ae_info *deferred_apple_events = NULL;
938
939/* List of suspended apple events, in order of expiration_tick. */
940static struct suspended_ae_info *suspended_apple_events = NULL;
941
930static void 942static void
931find_event_binding_fun (key, binding, args, data) 943find_event_binding_fun (key, binding, args, data)
932 Lisp_Object key, binding, args; 944 Lisp_Object key, binding, args;
@@ -1003,6 +1015,12 @@ defer_apple_events (apple_event, reply)
1003 const AppleEvent *apple_event, *reply; 1015 const AppleEvent *apple_event, *reply;
1004{ 1016{
1005 OSErr err; 1017 OSErr err;
1018 struct suspended_ae_info *new;
1019
1020 new = xmalloc (sizeof (struct suspended_ae_info));
1021 bzero (new, sizeof (struct suspended_ae_info));
1022 new->apple_event.descriptorType = typeNull;
1023 new->reply.descriptorType = typeNull;
1006 1024
1007 err = AESuspendTheCurrentEvent (apple_event); 1025 err = AESuspendTheCurrentEvent (apple_event);
1008 1026
@@ -1011,30 +1029,88 @@ defer_apple_events (apple_event, reply)
1011 manual says it doesn't. Anyway we create copies of them and save 1029 manual says it doesn't. Anyway we create copies of them and save
1012 them in `deferred_apple_events'. */ 1030 them in `deferred_apple_events'. */
1013 if (err == noErr) 1031 if (err == noErr)
1032 err = AEDuplicateDesc (apple_event, &new->apple_event);
1033 if (err == noErr)
1034 err = AEDuplicateDesc (reply, &new->reply);
1035 if (err == noErr)
1014 { 1036 {
1015 if (deferred_apple_events.buf == NULL) 1037 new->next = deferred_apple_events;
1016 { 1038 deferred_apple_events = new;
1017 deferred_apple_events.size = 16; 1039 }
1018 deferred_apple_events.count = 0; 1040 else
1019 deferred_apple_events.buf = 1041 {
1020 xmalloc (sizeof (AppleEvent) * deferred_apple_events.size); 1042 AEDisposeDesc (&new->apple_event);
1021 } 1043 AEDisposeDesc (&new->reply);
1022 else if (deferred_apple_events.count == deferred_apple_events.size) 1044 xfree (new);
1045 }
1046
1047 return err;
1048}
1049
1050static OSErr
1051mac_handle_apple_event_1 (class, id, apple_event, reply)
1052 Lisp_Object class, id;
1053 const AppleEvent *apple_event;
1054 AppleEvent *reply;
1055{
1056 OSErr err;
1057 static UInt32 suspension_id = 0;
1058 struct suspended_ae_info *new;
1059
1060 new = xmalloc (sizeof (struct suspended_ae_info));
1061 bzero (new, sizeof (struct suspended_ae_info));
1062 new->apple_event.descriptorType = typeNull;
1063 new->reply.descriptorType = typeNull;
1064
1065 err = AESuspendTheCurrentEvent (apple_event);
1066 if (err == noErr)
1067 err = AEDuplicateDesc (apple_event, &new->apple_event);
1068 if (err == noErr)
1069 err = AEDuplicateDesc (reply, &new->reply);
1070 if (err == noErr)
1071 err = AEPutAttributePtr (&new->apple_event, KEY_EMACS_SUSPENSION_ID_ATTR,
1072 typeUInt32, &suspension_id, sizeof (UInt32));
1073 if (err == noErr)
1074 {
1075 OSErr err1;
1076 SInt32 reply_requested;
1077
1078 err1 = AEGetAttributePtr (&new->apple_event, keyReplyRequestedAttr,
1079 typeSInt32, NULL, &reply_requested,
1080 sizeof (SInt32), NULL);
1081 if (err1 != noErr)
1023 { 1082 {
1024 deferred_apple_events.size *= 2; 1083 /* Emulate keyReplyRequestedAttr in older versions. */
1025 deferred_apple_events.buf 1084 reply_requested = reply->descriptorType != typeNull;
1026 = xrealloc (deferred_apple_events.buf, 1085 err = AEPutAttributePtr (&new->apple_event, keyReplyRequestedAttr,
1027 sizeof (AppleEvent) * deferred_apple_events.size); 1086 typeSInt32, &reply_requested,
1087 sizeof (SInt32));
1028 } 1088 }
1029 } 1089 }
1030
1031 if (err == noErr) 1090 if (err == noErr)
1032 { 1091 {
1033 int count = deferred_apple_events.count; 1092 SInt32 timeout = 0;
1093 struct suspended_ae_info **p;
1094
1095 new->suspension_id = suspension_id;
1096 suspension_id++;
1097 err = AEGetAttributePtr (apple_event, keyTimeoutAttr, typeSInt32,
1098 NULL, &timeout, sizeof (SInt32), NULL);
1099 new->expiration_tick = TickCount () + timeout;
1100
1101 for (p = &suspended_apple_events; *p; p = &(*p)->next)
1102 if ((*p)->expiration_tick >= new->expiration_tick)
1103 break;
1104 new->next = *p;
1105 *p = new;
1034 1106
1035 AEDuplicateDesc (apple_event, deferred_apple_events.buf + count); 1107 mac_store_apple_event (class, id, &new->apple_event);
1036 AEDuplicateDesc (reply, deferred_apple_events.buf + count + 1); 1108 }
1037 deferred_apple_events.count += 2; 1109 else
1110 {
1111 AEDisposeDesc (&new->reply);
1112 AEDisposeDesc (&new->apple_event);
1113 xfree (new);
1038 } 1114 }
1039 1115
1040 return err; 1116 return err;
@@ -1047,17 +1123,11 @@ mac_handle_apple_event (apple_event, reply, refcon)
1047 SInt32 refcon; 1123 SInt32 refcon;
1048{ 1124{
1049 OSErr err; 1125 OSErr err;
1126 UInt32 suspension_id;
1050 AEEventClass event_class; 1127 AEEventClass event_class;
1051 AEEventID event_id; 1128 AEEventID event_id;
1052 Lisp_Object class_key, id_key, binding; 1129 Lisp_Object class_key, id_key, binding;
1053 1130
1054 /* We can't handle an Apple event that requests a reply, but this
1055 seems to be too restrictive. */
1056#if 0
1057 if (reply->descriptorType != typeNull)
1058 return errAEEventNotHandled;
1059#endif
1060
1061 if (!mac_ready_for_apple_events) 1131 if (!mac_ready_for_apple_events)
1062 { 1132 {
1063 err = defer_apple_events (apple_event, reply); 1133 err = defer_apple_events (apple_event, reply);
@@ -1066,6 +1136,13 @@ mac_handle_apple_event (apple_event, reply, refcon)
1066 return noErr; 1136 return noErr;
1067 } 1137 }
1068 1138
1139 err = AEGetAttributePtr (apple_event, KEY_EMACS_SUSPENSION_ID_ATTR,
1140 typeUInt32, NULL,
1141 &suspension_id, sizeof (UInt32), NULL);
1142 if (err == noErr)
1143 /* Previously suspended event. Pass it to the next handler. */
1144 return errAEEventNotHandled;
1145
1069 err = AEGetAttributePtr (apple_event, keyEventClassAttr, typeType, NULL, 1146 err = AEGetAttributePtr (apple_event, keyEventClassAttr, typeType, NULL,
1070 &event_class, sizeof (AEEventClass), NULL); 1147 &event_class, sizeof (AEEventClass), NULL);
1071 if (err == noErr) 1148 if (err == noErr)
@@ -1079,11 +1156,47 @@ mac_handle_apple_event (apple_event, reply, refcon)
1079 { 1156 {
1080 if (INTEGERP (binding)) 1157 if (INTEGERP (binding))
1081 return XINT (binding); 1158 return XINT (binding);
1082 mac_store_apple_event (class_key, id_key, apple_event); 1159 err = mac_handle_apple_event_1 (class_key, id_key,
1083 return noErr; 1160 apple_event, reply);
1084 } 1161 }
1085 } 1162 }
1086 return errAEEventNotHandled; 1163 if (err == noErr)
1164 return noErr;
1165 else
1166 return errAEEventNotHandled;
1167}
1168
1169static int
1170cleanup_suspended_apple_events (head, all_p)
1171 struct suspended_ae_info **head;
1172 int all_p;
1173{
1174 UInt32 current_tick = TickCount (), nresumed = 0;
1175 struct suspended_ae_info *p, *next;
1176
1177 for (p = *head; p; p = next)
1178 {
1179 if (!all_p && p->expiration_tick > current_tick)
1180 break;
1181 AESetTheCurrentEvent (&p->apple_event);
1182 AEResumeTheCurrentEvent (&p->apple_event, &p->reply,
1183 (AEEventHandlerUPP) kAENoDispatch, 0);
1184 AEDisposeDesc (&p->reply);
1185 AEDisposeDesc (&p->apple_event);
1186 nresumed++;
1187 next = p->next;
1188 xfree (p);
1189 }
1190 *head = p;
1191
1192 return nresumed;
1193}
1194
1195static void
1196cleanup_all_suspended_apple_events ()
1197{
1198 cleanup_suspended_apple_events (&deferred_apple_events, 1);
1199 cleanup_suspended_apple_events (&suspended_apple_events, 1);
1087} 1200}
1088 1201
1089void 1202void
@@ -1109,34 +1222,190 @@ init_apple_event_handler ()
1109 0L, false); 1222 0L, false);
1110 if (err != noErr) 1223 if (err != noErr)
1111 abort (); 1224 abort ();
1225
1226 atexit (cleanup_all_suspended_apple_events);
1112} 1227}
1113 1228
1229static UInt32
1230get_suspension_id (apple_event)
1231 Lisp_Object apple_event;
1232{
1233 Lisp_Object tem;
1234
1235 CHECK_CONS (apple_event);
1236 CHECK_STRING_CAR (apple_event);
1237 if (SBYTES (XCAR (apple_event)) != 4
1238 || strcmp (SDATA (XCAR (apple_event)), "aevt") != 0)
1239 error ("Not an apple event");
1240
1241 tem = assq_no_quit (Qemacs_suspension_id, XCDR (apple_event));
1242 if (NILP (tem))
1243 error ("Suspension ID not available");
1244
1245 tem = XCDR (tem);
1246 if (!(CONSP (tem)
1247 && STRINGP (XCAR (tem)) && SBYTES (XCAR (tem)) == 4
1248 && strcmp (SDATA (XCAR (tem)), "magn") == 0
1249 && STRINGP (XCDR (tem)) && SBYTES (XCDR (tem)) == 4))
1250 error ("Bad suspension ID format");
1251
1252 return *((UInt32 *) SDATA (XCDR (tem)));
1253}
1254
1255
1114DEFUN ("mac-process-deferred-apple-events", Fmac_process_deferred_apple_events, Smac_process_deferred_apple_events, 0, 0, 0, 1256DEFUN ("mac-process-deferred-apple-events", Fmac_process_deferred_apple_events, Smac_process_deferred_apple_events, 0, 0, 0,
1115 doc: /* Process Apple events that are deferred at the startup time. */) 1257 doc: /* Process Apple events that are deferred at the startup time. */)
1116 () 1258 ()
1117{ 1259{
1118 Lisp_Object result = Qnil;
1119 long i;
1120
1121 if (mac_ready_for_apple_events) 1260 if (mac_ready_for_apple_events)
1122 return Qnil; 1261 return Qnil;
1123 1262
1124 BLOCK_INPUT; 1263 BLOCK_INPUT;
1125 mac_ready_for_apple_events = 1; 1264 mac_ready_for_apple_events = 1;
1126 if (deferred_apple_events.buf) 1265 if (deferred_apple_events)
1127 { 1266 {
1128 for (i = 0; i < deferred_apple_events.count; i += 2) 1267 struct suspended_ae_info *prev, *tail, *next;
1268
1269 /* `nreverse' deferred_apple_events. */
1270 prev = NULL;
1271 for (tail = deferred_apple_events; tail; tail = next)
1129 { 1272 {
1130 AEResumeTheCurrentEvent (deferred_apple_events.buf + i, 1273 next = tail->next;
1131 deferred_apple_events.buf + i + 1, 1274 tail->next = prev;
1275 prev = tail;
1276 }
1277
1278 /* Now `prev' points to the first cell. */
1279 for (tail = prev; tail; tail = next)
1280 {
1281 next = tail->next;
1282 AEResumeTheCurrentEvent (&tail->apple_event, &tail->reply,
1132 ((AEEventHandlerUPP) 1283 ((AEEventHandlerUPP)
1133 kAEUseStandardDispatch), 0); 1284 kAEUseStandardDispatch), 0);
1134 AEDisposeDesc (deferred_apple_events.buf + i); 1285 AEDisposeDesc (&tail->reply);
1135 AEDisposeDesc (deferred_apple_events.buf + i + 1); 1286 AEDisposeDesc (&tail->apple_event);
1287 xfree (tail);
1136 } 1288 }
1137 xfree (deferred_apple_events.buf);
1138 bzero (&deferred_apple_events, sizeof (deferred_apple_events));
1139 1289
1290 deferred_apple_events = NULL;
1291 }
1292 UNBLOCK_INPUT;
1293
1294 return Qt;
1295}
1296
1297DEFUN ("mac-cleanup-expired-apple-events", Fmac_cleanup_expired_apple_events, Smac_cleanup_expired_apple_events, 0, 0, 0,
1298 doc: /* Clean up expired Apple events.
1299Return the number of expired events. */)
1300 ()
1301{
1302 int nexpired;
1303
1304 BLOCK_INPUT;
1305 nexpired = cleanup_suspended_apple_events (&suspended_apple_events, 0);
1306 UNBLOCK_INPUT;
1307
1308 return make_number (nexpired);
1309}
1310
1311DEFUN ("mac-ae-set-reply-parameter", Fmac_ae_set_reply_parameter, Smac_ae_set_reply_parameter, 3, 3, 0,
1312 doc: /* Set parameter KEYWORD to DESCRIPTOR on reply of APPLE-EVENT.
1313KEYWORD is a 4-byte string. DESCRIPTOR is a Lisp representation of an
1314Apple event descriptor. It has the form of (TYPE . DATA), where TYPE
1315is a 4-byte string. Valid format of DATA is as follows:
1316
1317 * If TYPE is "null", then DATA is nil.
1318 * If TYPE is "list", then DATA is a list (DESCRIPTOR1 ... DESCRIPTORn).
1319 * If TYPE is "reco", then DATA is a list ((KEYWORD1 . DESCRIPTOR1)
1320 ... (KEYWORDn . DESCRIPTORn)).
1321 * If TYPE is "aevt", then DATA is ignored and the descriptor is
1322 treated as null.
1323 * Otherwise, DATA is a string.
1324
1325If a (sub-)descriptor is in an invalid format, it is silently treated
1326as null.
1327
1328Return t if the parameter is successfully set. Otherwise return nil. */)
1329 (apple_event, keyword, descriptor)
1330 Lisp_Object apple_event, keyword, descriptor;
1331{
1332 Lisp_Object result = Qnil;
1333 UInt32 suspension_id;
1334 struct suspended_ae_info *p;
1335
1336 suspension_id = get_suspension_id (apple_event);
1337
1338 CHECK_STRING (keyword);
1339 if (SBYTES (keyword) != 4)
1340 error ("Apple event keyword must be a 4-byte string: %s",
1341 SDATA (keyword));
1342
1343 BLOCK_INPUT;
1344 for (p = suspended_apple_events; p; p = p->next)
1345 if (p->suspension_id == suspension_id)
1346 break;
1347 if (p && p->reply.descriptorType != typeNull)
1348 {
1349 OSErr err;
1350
1351 err = mac_ae_put_lisp (&p->reply,
1352 EndianU32_BtoN (*((UInt32 *) SDATA (keyword))),
1353 descriptor);
1354 if (err == noErr)
1355 result = Qt;
1356 }
1357 UNBLOCK_INPUT;
1358
1359 return result;
1360}
1361
1362DEFUN ("mac-resume-apple-event", Fmac_resume_apple_event, Smac_resume_apple_event, 1, 2, 0,
1363 doc: /* Resume handling of APPLE-EVENT.
1364Every Apple event handled by the Lisp interpreter is suspended first.
1365This function resumes such a suspended event either to complete Apple
1366event handling to give a reply, or to redispatch it to other handlers.
1367
1368If optional ERROR-CODE is an integer, it specifies the error number
1369that is set in the reply. If ERROR-CODE is t, the resumed event is
1370handled with the standard dispatching mechanism, but it is not handled
1371by Emacs again, thus it is redispatched to other handlers.
1372
1373Return t if APPLE-EVENT is successfully resumed. Otherwise return
1374nil, which means the event is already resumed or expired. */)
1375 (apple_event, error_code)
1376 Lisp_Object apple_event, error_code;
1377{
1378 Lisp_Object result = Qnil;
1379 UInt32 suspension_id;
1380 struct suspended_ae_info **p, *ae;
1381
1382 suspension_id = get_suspension_id (apple_event);
1383
1384 BLOCK_INPUT;
1385 for (p = &suspended_apple_events; *p; p = &(*p)->next)
1386 if ((*p)->suspension_id == suspension_id)
1387 break;
1388 if (*p)
1389 {
1390 ae = *p;
1391 *p = (*p)->next;
1392 if (INTEGERP (error_code)
1393 && ae->apple_event.descriptorType != typeNull)
1394 {
1395 SInt32 errn = XINT (error_code);
1396
1397 AEPutParamPtr (&ae->reply, keyErrorNumber, typeSInt32,
1398 &errn, sizeof (SInt32));
1399 }
1400 AESetTheCurrentEvent (&ae->apple_event);
1401 AEResumeTheCurrentEvent (&ae->apple_event, &ae->reply,
1402 ((AEEventHandlerUPP)
1403 (EQ (error_code, Qt) ?
1404 kAEUseStandardDispatch : kAENoDispatch)),
1405 0);
1406 AEDisposeDesc (&ae->reply);
1407 AEDisposeDesc (&ae->apple_event);
1408 xfree (ae);
1140 result = Qt; 1409 result = Qt;
1141 } 1410 }
1142 UNBLOCK_INPUT; 1411 UNBLOCK_INPUT;
@@ -1145,6 +1414,9 @@ DEFUN ("mac-process-deferred-apple-events", Fmac_process_deferred_apple_events,
1145} 1414}
1146 1415
1147 1416
1417/***********************************************************************
1418 Drag and drop support
1419***********************************************************************/
1148#if TARGET_API_MAC_CARBON 1420#if TARGET_API_MAC_CARBON
1149static Lisp_Object Vmac_dnd_known_types; 1421static Lisp_Object Vmac_dnd_known_types;
1150static pascal OSErr mac_do_track_drag P_ ((DragTrackingMessage, WindowRef, 1422static pascal OSErr mac_do_track_drag P_ ((DragTrackingMessage, WindowRef,
@@ -1337,6 +1609,9 @@ remove_drag_handler (window)
1337} 1609}
1338 1610
1339 1611
1612/***********************************************************************
1613 Services menu support
1614***********************************************************************/
1340#ifdef MAC_OSX 1615#ifdef MAC_OSX
1341void 1616void
1342init_service_handler () 1617init_service_handler ()
@@ -1554,6 +1829,9 @@ syms_of_macselect ()
1554 defsubr (&Sx_selection_owner_p); 1829 defsubr (&Sx_selection_owner_p);
1555 defsubr (&Sx_selection_exists_p); 1830 defsubr (&Sx_selection_exists_p);
1556 defsubr (&Smac_process_deferred_apple_events); 1831 defsubr (&Smac_process_deferred_apple_events);
1832 defsubr (&Smac_cleanup_expired_apple_events);
1833 defsubr (&Smac_resume_apple_event);
1834 defsubr (&Smac_ae_set_reply_parameter);
1557 1835
1558 Vselection_alist = Qnil; 1836 Vselection_alist = Qnil;
1559 staticpro (&Vselection_alist); 1837 staticpro (&Vselection_alist);
@@ -1635,6 +1913,9 @@ The types are chosen in the order they appear in the list. */);
1635 1913
1636 Qmac_apple_event_id = intern ("mac-apple-event-id"); 1914 Qmac_apple_event_id = intern ("mac-apple-event-id");
1637 staticpro (&Qmac_apple_event_id); 1915 staticpro (&Qmac_apple_event_id);
1916
1917 Qemacs_suspension_id = intern ("emacs-suspension-id");
1918 staticpro (&Qemacs_suspension_id);
1638} 1919}
1639 1920
1640/* arch-tag: f3c91ad8-99e0-4bd6-9eef-251b2f848732 1921/* arch-tag: f3c91ad8-99e0-4bd6-9eef-251b2f848732