My Project
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules
apt.c
Go to the documentation of this file.
1 /*
2  apt.c _ Applet/NS shell interaction
3 */
4 
5 #include <stdlib.h>
6 #include <string.h>
7 #include <3ds.h>
8 
9 #define APT_HANDLER_STACKSIZE (0x1000)
10 
11 //TODO : better place to put this ?
12 extern u32 __apt_appid;
13 extern u32 __system_runflags;
14 
16 
17 static char *__apt_servicestr = NULL;
18 static char *__apt_servicenames[3] = {"APT:U", "APT:S", "APT:A"};
19 
20 static u32 __apt_new3dsflag_initialized = 0;
21 static u8 __apt_new3dsflag = 0;
22 
26 
28 u64 aptEventHandlerStack[APT_HANDLER_STACKSIZE/8]; // u64 so that it's 8-byte aligned
29 
36 
37 u32 aptParameters[0x1000/4]; //TEMP
38 
39 static u32 __ns_capinfo[0x20>>2];
40 
41 static NS_APPID __apt_launchapplet_appID;
42 static Handle __apt_launchapplet_inhandle;
43 static u32 *__apt_launchapplet_parambuf;
44 static u32 __apt_launchapplet_parambufsize;
45 
46 static void aptAppStarted(void);
47 
48 static Result __apt_initservicehandle()
49 {
50  Result ret=0;
51  u32 i;
52 
53  if(__apt_servicestr)
54  {
55  return srvGetServiceHandle(&aptuHandle, __apt_servicestr);
56  }
57 
58  for(i=0; i<3; i++)
59  {
60  ret = srvGetServiceHandle(&aptuHandle, __apt_servicenames[i]);
61  if(ret==0)
62  {
63  __apt_servicestr = __apt_servicenames[i];
64  return ret;
65  }
66  }
67 
68  return ret;
69 }
70 
71 void aptInitCaptureInfo(u32 *ns_capinfo)
72 {
73  u32 tmp=0;
74  u32 main_pixsz, sub_pixsz;
75  GSP_CaptureInfo gspcapinfo;
76 
77  memset(&gspcapinfo, 0, sizeof(GSP_CaptureInfo));
78 
79  // Get display-capture info from GSP.
80  GSPGPU_ImportDisplayCaptureInfo(NULL, &gspcapinfo);
81 
82  // Fill in display-capture info for NS.
83  if(gspcapinfo.screencapture[0].framebuf0_vaddr != gspcapinfo.screencapture[1].framebuf0_vaddr)ns_capinfo[1] = 1;
84 
85  ns_capinfo[4] = gspcapinfo.screencapture[0].format & 0x7;
86  ns_capinfo[7] = gspcapinfo.screencapture[1].format & 0x7;
87 
88  main_pixsz = (ns_capinfo[4] < 2) ? 3 : 2;
89  sub_pixsz = (ns_capinfo[7] < 2) ? 3 : 2;
90 
91  ns_capinfo[2] = sub_pixsz * 0x14000;
92  ns_capinfo[3] = ns_capinfo[2];
93 
94  if(ns_capinfo[1])ns_capinfo[3] = main_pixsz * 0x19000 + ns_capinfo[2];
95 
96  tmp = main_pixsz * 0x19000 + ns_capinfo[3];
97  ns_capinfo[0] = main_pixsz * 0x7000 + tmp;
98 }
99 
101 {
104 }
105 
107 {
108  u8 buf1[4], buf2[4];
109 
110  memset(buf1, 0, 4);
111 
112  buf1[0]=0x10;
113  aptOpenSession();
114  APT_AppletUtility(NULL, NULL, 0x7, 0x4, buf1, 0x1, buf2);
115  aptCloseSession();
116 
117  buf1[0]=0x00;
118  aptOpenSession();
119  APT_AppletUtility(NULL, NULL, 0x4, 0x1, buf1, 0x1, buf2);
120  aptCloseSession();
121 
122  buf1[0]=0x01;
123  aptOpenSession();
124  APT_AppletUtility(NULL, NULL, 0x7, 0x4, buf1, 0x1, buf2);
125  aptCloseSession();
126 
127  buf1[0]=0x00;
128  aptOpenSession();
129  APT_AppletUtility(NULL, NULL, 0x4, 0x1, buf1, 0x1, buf2);
130  aptCloseSession();
131 
132  aptOpenSession();
133  APT_AppletUtility(NULL, NULL, 0x4, 0x1, buf1, 0x1, buf2);
134  aptCloseSession();
135 }
136 
138 {
139  NS_APPID menu_appid;
140 
141  aptOpenSession();
142  APT_GetAppletManInfo(NULL, 0xff, NULL, NULL, &menu_appid, NULL);
143  aptCloseSession();
144 
145  return menu_appid;
146 }
147 
149 {
150  NS_APPID menu_appid;
151  u32 tmp0 = 1, tmp1 = 0;
152 
154  {
157  return;
158  }
159 
160  // This is only executed when ret-to-menu was triggered via the home-button, not the power-button.
161  if(aptGetStatusPower() == 0)
162  {
163  aptOpenSession();
164  APT_AppletUtility(NULL, NULL, 0x6, 0x4, (u8*)&tmp0, 0x1, (u8*)&tmp1);
165  aptCloseSession();
166  }
167 
168  // Prepare for return to menu
169  aptOpenSession();
171  aptCloseSession();
172 
173  // Set status to SUSPENDED.
176 
177  // Save Vram
179 
180  // Capture screen.
181  memset(__ns_capinfo, 0, 0x20);
182 
183  aptInitCaptureInfo(__ns_capinfo);
184 
185  menu_appid = aptGetMenuAppID();
186 
187  // Send capture-screen info to menu.
188  aptOpenSession();
189  APT_SendParameter(NULL, currentAppId, menu_appid, 0x20, __ns_capinfo, 0x0, 0x10);
190  aptCloseSession();
191 
192  aptOpenSession();
193  APT_SendCaptureBufferInfo(NULL, 0x20, __ns_capinfo);
194  aptCloseSession();
195 
196  // Release GSP module.
197  GSPGPU_ReleaseRight(NULL);
198 
199  // Jump to menu!
200  aptOpenSession();
201  APT_JumpToHomeMenu(NULL, 0x0, 0x0, 0x0);
202  aptCloseSession();
203 
204  // Wait for return to application.
205  aptOpenSession();
207  aptCloseSession();
208 
209  // This is only executed when ret-to-menu was triggered via the home-button, not the power-button.
210  if(aptGetStatusPower() == 0)
211  {
212  tmp0 = 0;
213  aptOpenSession();
214  APT_AppletUtility(NULL, NULL, 0x4, 0x1, (u8*)&tmp0, 0x1, (u8*)&tmp1);
215  aptCloseSession();
216  }
217 
219 }
220 
222 {
223  u8 buf1[4], buf2[4];
224 
225  memset(buf1, 0, 4);
226 
227  // Set status to SUSPENDED.
230 
231  aptOpenSession();
232  APT_SendCaptureBufferInfo(NULL, 0x20, __ns_capinfo);
233  aptCloseSession();
234 
235  aptOpenSession();
236  APT_ReplySleepQuery(NULL, currentAppId, 0x0);
237  aptCloseSession();
238 
239  aptOpenSession();
240  APT_StartLibraryApplet(NULL, __apt_launchapplet_appID, __apt_launchapplet_inhandle, __apt_launchapplet_parambuf, __apt_launchapplet_parambufsize);
241  aptCloseSession();
242 
243  buf1[0]=0x00;
244  aptOpenSession();
245  APT_AppletUtility(NULL, NULL, 0x4, 0x1, buf1, 0x1, buf2);
246  aptCloseSession();
247 
248  aptOpenSession();
250  aptCloseSession();
251 
252  buf1[0]=0x00;
253  aptOpenSession();
254  APT_AppletUtility(NULL, NULL, 0x4, 0x1, buf1, 0x1, buf2);
255  aptCloseSession();
256 }
257 
259 {
261 
262  GSPGPU_AcquireRight(NULL, 0x0);
264 
267 }
268 
269 static void __handle_notification() {
270  u8 type;
271  Result ret=0;
272 
273  // Get notification type.
274  aptOpenSession();
275  ret = APT_InquireNotification(NULL, currentAppId, &type);
276  aptCloseSession();
277 
278  if(ret!=0)return;
279 
280  switch(type)
281  {
284  // The main thread should call aptReturnToMenu() when the status gets set to this.
285  if(aptGetStatus() == APP_RUNNING)
286  {
287  aptOpenSession();
288  APT_ReplySleepQuery(NULL, currentAppId, 0x0);
289  aptCloseSession();
290 
294  }
295  break;
296 
298  // Reply to sleep-request.
303 
304  aptOpenSession();
305  APT_ReplySleepQuery(NULL, currentAppId, 0x1);
306  aptCloseSession();
307  break;
308 
311  {
312  // Report into sleep-mode.
314 
315  aptOpenSession();
317  aptCloseSession();
318  }
319  break;
320 
321  // Leaving sleep-mode.
322  case APTSIGNAL_WAKEUP:
323  if(aptGetStatus() == APP_SLEEPMODE)
324  {
326 
327  // Restore old aptStatus.
329  }
330  break;
331  }
332 }
333 
334 static bool __handle_incoming_parameter() {
335  u8 type;
336 
337  aptOpenSession();
338  APT_ReceiveParameter(NULL, currentAppId, 0x1000, aptParameters, NULL, &type);
339  aptCloseSession();
340 
341  switch(type)
342  {
343  case 0x1: // Application just started.
344  aptAppStarted();
345  return true;
346 
347  case 0x3: // "Launched library applet finished loading"
349  return true;
350  case 0xA: // "Launched library applet closed"
351  if(__apt_launchapplet_parambuf && __apt_launchapplet_parambufsize)memcpy(__apt_launchapplet_parambuf, aptParameters, __apt_launchapplet_parambufsize);
353  return true;
354  case 0xB: // Just returned from menu.
355  GSPGPU_AcquireRight(NULL, 0x0);
359  return true;
360 
361  case 0xC: // Exiting application.
363  return false;
364  }
365 
366  return true;
367 }
368 
370 {
371  bool runThread = true;
372 
373  while(runThread)
374  {
375  s32 syncedID = 0;
376  svcWaitSynchronizationN(&syncedID, aptEvents, 2, 0, U64_MAX);
377  svcClearEvent(aptEvents[syncedID]);
378 
379  switch(syncedID)
380  {
381  // Event 0 means we got a signal from NS (home button, power button etc).
382  case 0x0:
383  __handle_notification();
384  break;
385  // Event 1 means we got an incoming parameter.
386  case 0x1:
387  runThread = __handle_incoming_parameter();
388  break;
389  // Event 2 means we should exit the thread (event will be added later).
390  case 0x2:
391  runThread = false;
392  break;
393  }
394  }
395 
396  svcExitThread();
397 }
398 
400 {
401  Result ret=0;
402 
403  // Initialize APT stuff, escape load screen.
404  ret = __apt_initservicehandle();
405  if(ret!=0)return ret;
406  if((ret=APT_GetLockHandle(&aptuHandle, 0x0, &aptLockHandle)))return ret;
408 
410 
413 
415  {
416  aptOpenSession();
417  if((ret=APT_Initialize(NULL, currentAppId, &aptEvents[0], &aptEvents[1])))return ret;
418  aptCloseSession();
419 
420  aptOpenSession();
421  if((ret=APT_Enable(NULL, 0x0)))return ret;
422  aptCloseSession();
423 
424  aptOpenSession();
425  if((ret=APT_NotifyToWait(NULL, currentAppId)))return ret;
426  aptCloseSession();
427 
428  // create APT event handler thread
430  (u32*)(&aptEventHandlerStack[APT_HANDLER_STACKSIZE/8]), 0x31, 0xfffffffe);
431  } else
432  aptAppStarted();
433 
434  return 0;
435 }
436 
437 void aptExit()
438 {
440 
441  // This is only executed when application-termination was triggered via the home-menu power-off screen.
442  if(aptGetStatusPower() == 1)
443  {
444  aptOpenSession();
445  APT_ReplySleepQuery(NULL, currentAppId, 0x0);
446  aptCloseSession();
447  }
448 
449  if(!(__system_runflags&RUNFLAG_APTWORKAROUND))
450  {
451  aptOpenSession();
453  aptCloseSession();
454 
455  aptOpenSession();
456  APT_CloseApplication(NULL, 0x0, 0x0, 0x0);
457  aptCloseSession();
458  }
459 
461 
465 }
466 
468 {
469  while(1)
470  {
471  //if(__system_runflags&RUNFLAG_APTWORKAROUND)__handle_notification();
472 
473  switch(aptGetStatus())
474  {
475  case APP_RUNNING:
476  return true;
477  case APP_EXITING:
478  return false;
479  case APP_SUSPENDING:
480  aptReturnToMenu();
481  break;
484  // Fall through
485  case APP_APPLETSTARTED:
487  break;
488  case APP_APPLETCLOSED:
489  aptAppletClosed();
490  break;
491  default:
492  //case APP_NOTINITIALIZED:
493  //case APP_SLEEPMODE:
495  break;
496  }
497  }
498 }
499 
500 void aptAppStarted()
501 {
502  u8 buf1[4], buf2[4];
503 
505  aptStatus=0;
507 
509 
511  {
512  memset(buf1, 0, 4);
513 
514  buf1[0] = 0x10;
515  aptOpenSession();
516  APT_AppletUtility(NULL, NULL, 0x7, 0x4, buf1, 0x1, buf2);
517  aptCloseSession();
518 
519  buf1[0] = 0x00;
520  aptOpenSession();
521  APT_AppletUtility(NULL, NULL, 0x4, 0x1, buf1, 0x1, buf2);
522  aptCloseSession();
523 
524  aptOpenSession();
525  APT_AppletUtility(NULL, NULL, 0x4, 0x1, buf1, 0x1, buf2);
526  aptCloseSession();
527  }
528 }
529 
531 {
532  APP_STATUS ret;
534  ret = aptStatus;
536  return ret;
537 }
538 
540 {
542 
543  aptStatus = status;
544 
545  //if(prevstatus != APP_NOTINITIALIZED)
546  //{
547  if(status == APP_RUNNING || status == APP_EXITING || status == APP_SLEEPMODE || status == APP_APPLETSTARTED || status == APP_APPLETCLOSED)
549  //}
550 
552 }
553 
555 {
556  u32 ret;
558  ret = aptStatusPower;
560  return ret;
561 }
562 
563 void aptSetStatusPower(u32 status)
564 {
566  aptStatusPower = status;
568 }
569 
571 {
572  //Result ret;
573 
575  __apt_initservicehandle();
576 }
577 
579 {
582 }
583 
585 {
587 }
588 
589 Result APT_GetLockHandle(Handle* handle, u16 flags, Handle* lockHandle)
590 {
591  if(!handle)handle=&aptuHandle;
592  u32* cmdbuf=getThreadCommandBuffer();
593  cmdbuf[0]=0x10040; //request header code
594  cmdbuf[1]=flags;
595 
596  Result ret=0;
597  if((ret=svcSendSyncRequest(*handle)))return ret;
598 
599  if(lockHandle)*lockHandle=cmdbuf[5];
600 
601  return cmdbuf[1];
602 }
603 
604 Result APT_Initialize(Handle* handle, NS_APPID appId, Handle* eventHandle1, Handle* eventHandle2)
605 {
606  if(!handle)handle=&aptuHandle;
607  u32* cmdbuf=getThreadCommandBuffer();
608  cmdbuf[0]=0x20080; //request header code
609  cmdbuf[1]=appId;
610  cmdbuf[2]=0x0;
611 
612  Result ret=0;
613  if((ret=svcSendSyncRequest(*handle)))return ret;
614 
615  if(eventHandle1)*eventHandle1=cmdbuf[3]; //return to menu event ?
616  if(eventHandle2)*eventHandle2=cmdbuf[4];
617 
618  return cmdbuf[1];
619 }
620 
622 {
623  if(!handle)handle=&aptuHandle;
624  u32* cmdbuf=getThreadCommandBuffer();
625  cmdbuf[0]=0x4E0000; //request header code
626 
627  Result ret=0;
628  if((ret=svcSendSyncRequest(*handle)))return ret;
629 
630  return cmdbuf[1];
631 }
632 
634 {
635  if(!handle)handle=&aptuHandle;
636  u32* cmdbuf=getThreadCommandBuffer();
637  cmdbuf[0]=0x30040; //request header code
638  cmdbuf[1]=a;
639 
640  Result ret=0;
641  if((ret=svcSendSyncRequest(*handle)))return ret;
642 
643  return cmdbuf[1];
644 }
645 
646 Result APT_GetAppletManInfo(Handle* handle, u8 inval, u8 *outval8, u32 *outval32, NS_APPID *menu_appid, NS_APPID *active_appid)
647 {
648  if(!handle)handle=&aptuHandle;
649  u32* cmdbuf=getThreadCommandBuffer();
650  cmdbuf[0]=0x00050040; //request header code
651  cmdbuf[1]=inval;
652 
653  Result ret=0;
654  if((ret=svcSendSyncRequest(*handle)))return ret;
655 
656  if(outval8)*outval8=cmdbuf[2];
657  if(outval32)*outval32=cmdbuf[3];
658  if(menu_appid)*menu_appid=cmdbuf[4];
659  if(active_appid)*active_appid=cmdbuf[5];
660 
661  return cmdbuf[1];
662 }
663 
664 Result APT_IsRegistered(Handle* handle, NS_APPID appID, u8* out)
665 {
666  if(!handle)handle=&aptuHandle;
667  u32* cmdbuf=getThreadCommandBuffer();
668  cmdbuf[0]=0x90040; //request header code
669  cmdbuf[1]=appID;
670 
671  Result ret=0;
672  if((ret=svcSendSyncRequest(*handle)))return ret;
673 
674  if(out)*out=cmdbuf[2];
675 
676  return cmdbuf[1];
677 }
678 
679 Result APT_InquireNotification(Handle* handle, u32 appID, u8* signalType)
680 {
681  if(!handle)handle=&aptuHandle;
682  u32* cmdbuf=getThreadCommandBuffer();
683  cmdbuf[0]=0xB0040; //request header code
684  cmdbuf[1]=appID;
685 
686  Result ret=0;
687  if((ret=svcSendSyncRequest(*handle)))return ret;
688 
689  if(signalType)*signalType=cmdbuf[2];
690 
691  return cmdbuf[1];
692 }
693 
695 {
696  if(!handle)handle=&aptuHandle;
697  u32* cmdbuf=getThreadCommandBuffer();
698  cmdbuf[0]=0x2b0000; //request header code
699 
700  Result ret=0;
701  if((ret=svcSendSyncRequest(*handle)))return ret;
702 
703  return cmdbuf[1];
704 }
705 
707 {
708  if(!handle)handle=&aptuHandle;
709  u32* cmdbuf=getThreadCommandBuffer();
710  cmdbuf[0]=0x2C0044; //request header code
711  cmdbuf[1]=a;
712  cmdbuf[2]=b;
713  cmdbuf[3]=c;
714  cmdbuf[4]=(b<<14)|2;
715 
716  Result ret=0;
717  if((ret=svcSendSyncRequest(*handle)))return ret;
718 
719  return cmdbuf[1];
720 }
721 
723 {
724  if(!handle)handle=&aptuHandle;
725  u32* cmdbuf=getThreadCommandBuffer();
726  cmdbuf[0]=0x430040; //request header code
727  cmdbuf[1]=appID;
728 
729  Result ret=0;
730  if((ret=svcSendSyncRequest(*handle)))return ret;
731 
732  return cmdbuf[1];
733 }
734 
735 Result APT_AppletUtility(Handle* handle, u32* out, u32 a, u32 size1, u8* buf1, u32 size2, u8* buf2)
736 {
737  if(!handle)handle=&aptuHandle;
738  u32* cmdbuf=getThreadCommandBuffer();
739  cmdbuf[0]=0x4B00C2; //request header code
740  cmdbuf[1]=a;
741  cmdbuf[2]=size1;
742  cmdbuf[3]=size2;
743  cmdbuf[4]=(size1<<14)|0x402;
744  cmdbuf[5]=(u32)buf1;
745 
746  cmdbuf[0+0x100/4]=(size2<<14)|2;
747  cmdbuf[1+0x100/4]=(u32)buf2;
748 
749  Result ret=0;
750  if((ret=svcSendSyncRequest(*handle)))return ret;
751 
752  if(out)*out=cmdbuf[2];
753 
754  return cmdbuf[1];
755 }
756 
757 Result APT_GlanceParameter(Handle* handle, NS_APPID appID, u32 bufferSize, u32* buffer, u32* actualSize, u8* signalType)
758 {
759  if(!handle)handle=&aptuHandle;
760  u32* cmdbuf=getThreadCommandBuffer();
761  cmdbuf[0]=0xE0080; //request header code
762  cmdbuf[1]=appID;
763  cmdbuf[2]=bufferSize;
764 
765  cmdbuf[0+0x100/4]=(bufferSize<<14)|2;
766  cmdbuf[1+0x100/4]=(u32)buffer;
767 
768  Result ret=0;
769  if((ret=svcSendSyncRequest(*handle)))return ret;
770 
771  if(signalType)*signalType=cmdbuf[3];
772  if(actualSize)*actualSize=cmdbuf[4];
773 
774  return cmdbuf[1];
775 }
776 
777 Result APT_ReceiveParameter(Handle* handle, NS_APPID appID, u32 bufferSize, u32* buffer, u32* actualSize, u8* signalType)
778 {
779  if(!handle)handle=&aptuHandle;
780  u32* cmdbuf=getThreadCommandBuffer();
781  cmdbuf[0]=0xD0080; //request header code
782  cmdbuf[1]=appID;
783  cmdbuf[2]=bufferSize;
784 
785  cmdbuf[0+0x100/4]=(bufferSize<<14)|2;
786  cmdbuf[1+0x100/4]=(u32)buffer;
787 
788  Result ret=0;
789  if((ret=svcSendSyncRequest(*handle)))return ret;
790 
791  if(signalType)*signalType=cmdbuf[3];
792  if(actualSize)*actualSize=cmdbuf[4];
793 
794  return cmdbuf[1];
795 }
796 
797 Result APT_SendParameter(Handle* handle, NS_APPID src_appID, NS_APPID dst_appID, u32 bufferSize, u32* buffer, Handle paramhandle, u8 signalType)
798 {
799  u32* cmdbuf=getThreadCommandBuffer();
800 
801  if(!handle)handle=&aptuHandle;
802 
803  cmdbuf[0] = 0x000C0104; //request header code
804  cmdbuf[1] = src_appID;
805  cmdbuf[2] = dst_appID;
806  cmdbuf[3] = signalType;
807  cmdbuf[4] = bufferSize;
808 
809  cmdbuf[5]=0x0;
810  cmdbuf[6] = paramhandle;
811 
812  cmdbuf[7] = (bufferSize<<14) | 2;
813  cmdbuf[8] = (u32)buffer;
814 
815  Result ret=0;
816  if((ret=svcSendSyncRequest(*handle)))return ret;
817 
818  return cmdbuf[1];
819 }
820 
821 Result APT_SendCaptureBufferInfo(Handle* handle, u32 bufferSize, u32* buffer)
822 {
823  u32* cmdbuf=getThreadCommandBuffer();
824 
825  if(!handle)handle=&aptuHandle;
826 
827  cmdbuf[0] = 0x00400042; //request header code
828  cmdbuf[1] = bufferSize;
829  cmdbuf[2] = (bufferSize<<14) | 2;
830  cmdbuf[3] = (u32)buffer;
831 
832  Result ret=0;
833  if((ret=svcSendSyncRequest(*handle)))return ret;
834 
835  return cmdbuf[1];
836 }
837 
839 {
840  if(!handle)handle=&aptuHandle;
841 
842  u32* cmdbuf=getThreadCommandBuffer();
843  cmdbuf[0]=0x3E0080; //request header code
844  cmdbuf[1]=appID;
845  cmdbuf[2]=a;
846 
847  Result ret=0;
848  if((ret=svcSendSyncRequest(*handle)))return ret;
849 
850  return cmdbuf[1];
851 }
852 
854 {
855  if(!handle)handle=&aptuHandle;
856 
857  u32* cmdbuf=getThreadCommandBuffer();
858  cmdbuf[0]=0x3F0040; //request header code
859  cmdbuf[1]=appID;
860 
861  Result ret=0;
862  if((ret=svcSendSyncRequest(*handle)))return ret;
863 
864  return cmdbuf[1];
865 }
866 
868 {
869  if(!handle)handle=&aptuHandle;
870 
871  u32* cmdbuf=getThreadCommandBuffer();
872  cmdbuf[0]=0x220040; //request header code
873  cmdbuf[1]=a;
874 
875  Result ret=0;
876  if((ret=svcSendSyncRequest(*handle)))return ret;
877 
878  return cmdbuf[1];
879 }
880 
882 {
883  if(!handle)handle=&aptuHandle;
884 
885  u32* cmdbuf=getThreadCommandBuffer();
886  cmdbuf[0]=0x270044; //request header code
887  cmdbuf[1]=a;
888  cmdbuf[2]=0x0;
889  cmdbuf[3]=b;
890  cmdbuf[4]=(a<<14)|2;
891  cmdbuf[5]=c;
892 
893  Result ret=0;
894  if((ret=svcSendSyncRequest(*handle)))return ret;
895 
896  return cmdbuf[1];
897 }
898 
899 //See http://3dbrew.org/APT:SetApplicationCpuTimeLimit
901 {
902  if(!handle)handle=&aptuHandle;
903 
904  u32* cmdbuf=getThreadCommandBuffer();
905  cmdbuf[0]=0x4F0080;
906  cmdbuf[1]=1;
907  cmdbuf[2]=percent;
908 
909  Result ret=0;
910  if((ret=svcSendSyncRequest(*handle)))return ret;
911 
912  return cmdbuf[1];
913 }
914 
916 {
917  if(!handle)handle=&aptuHandle;
918 
919  u32* cmdbuf=getThreadCommandBuffer();
920  cmdbuf[0]=0x500040;
921  cmdbuf[1]=1;
922 
923  Result ret=0;
924  if((ret=svcSendSyncRequest(*handle)))return ret;
925 
926  if(percent)*percent=cmdbuf[2];
927 
928  return cmdbuf[1];
929 }
930 
932 {
933  if(!handle)handle=&aptuHandle;
934 
935  u32* cmdbuf=getThreadCommandBuffer();
936  cmdbuf[0]=0x01010000;
937 
938  Result ret=0;
939  if((ret=svcSendSyncRequest(*handle)))return ret;
940 
941  if(out)
942  {
943  *out = 0;
944  if(ret==0)*out=cmdbuf[2];
945  }
946 
947  return cmdbuf[1];
948 }
949 
951 {
952  if(!handle)handle=&aptuHandle;
953 
954  u32* cmdbuf=getThreadCommandBuffer();
955  cmdbuf[0]=0x01020000;
956 
957  Result ret=0;
958  if((ret=svcSendSyncRequest(*handle)))return ret;
959 
960  if(out)
961  {
962  *out = 0;
963  if(ret==0)*out=cmdbuf[2];
964  }
965 
966  return cmdbuf[1];
967 }
968 
970 {
971  Result ret=0;
972 
973  if(__apt_new3dsflag_initialized)
974  {
975  *out = __apt_new3dsflag;
976  return 0;
977  }
978 
979  aptOpenSession();
981  {
982  ret = APT_CheckNew3DS_Application(NULL, out);
983  }
984  else
985  {
986  ret = APT_CheckNew3DS_System(NULL, out);
987  }
988  aptCloseSession();
989 
990  __apt_new3dsflag_initialized = 1;
991  __apt_new3dsflag = *out;
992 
993  return ret;
994 }
995 
996 Result APT_PrepareToDoAppJump(Handle* handle, u8 flags, u64 programID, u8 mediatype)
997 {
998  if(!handle)handle=&aptuHandle;
999 
1000  u32* cmdbuf=getThreadCommandBuffer();
1001  cmdbuf[0]=0x310100; //request header code
1002  cmdbuf[1]=flags;
1003  cmdbuf[2]=(u32)programID;
1004  cmdbuf[3]=(u32)(programID>>32);
1005  cmdbuf[4]=mediatype;
1006 
1007  Result ret=0;
1008  if((ret=svcSendSyncRequest(*handle)))return ret;
1009 
1010  return cmdbuf[1];
1011 }
1012 
1013 Result APT_DoAppJump(Handle* handle, u32 NSbuf0Size, u32 NSbuf1Size, u8 *NSbuf0Ptr, u8 *NSbuf1Ptr)
1014 {
1015  if(!handle)handle=&aptuHandle;
1016 
1017  u32* cmdbuf=getThreadCommandBuffer();
1018  cmdbuf[0]=0x320084; //request header code
1019  cmdbuf[1]=NSbuf0Size;
1020  cmdbuf[2]=NSbuf1Size;
1021  cmdbuf[3]=(NSbuf0Size<<14)|2;
1022  cmdbuf[4]=(u32)NSbuf0Ptr;
1023  cmdbuf[5]=(NSbuf1Size<<14)|0x802;
1024  cmdbuf[6]=(u32)NSbuf1Ptr;
1025 
1026  Result ret=0;
1027  if((ret=svcSendSyncRequest(*handle)))return ret;
1028 
1029  return cmdbuf[1];
1030 }
1031 
1033 {
1034  if(!handle)handle=&aptuHandle;
1035 
1036  u32* cmdbuf=getThreadCommandBuffer();
1037  cmdbuf[0]=0x180040; //request header code
1038  cmdbuf[1]=appID;
1039 
1040  Result ret=0;
1041  if((ret=svcSendSyncRequest(*handle)))return ret;
1042 
1043  return cmdbuf[1];
1044 }
1045 
1046 Result APT_StartLibraryApplet(Handle* handle, NS_APPID appID, Handle inhandle, u32 *parambuf, u32 parambufsize)
1047 {
1048  if(!handle)handle=&aptuHandle;
1049 
1050  u32* cmdbuf=getThreadCommandBuffer();
1051  cmdbuf[0]=0x1E0084; //request header code
1052  cmdbuf[1]=appID;
1053  cmdbuf[2]=parambufsize;
1054  cmdbuf[3]=0;
1055  cmdbuf[4]=inhandle;
1056  cmdbuf[5]=(parambufsize<<14)|2;
1057  cmdbuf[6]=(u32)parambuf;
1058 
1059  Result ret=0;
1060  if((ret=svcSendSyncRequest(*handle)))return ret;
1061 
1062  return cmdbuf[1];
1063 }
1064 
1065 Result APT_LaunchLibraryApplet(NS_APPID appID, Handle inhandle, u32 *parambuf, u32 parambufsize)
1066 {
1067  Result ret=0;
1068  u8 tmp=0;
1069 
1070  u8 buf1[4];
1071  u8 buf2[4];
1072 
1073  aptOpenSession();
1075  aptCloseSession();
1076 
1077  memset(buf1, 0, 4);
1078  aptOpenSession();
1079  APT_AppletUtility(NULL, NULL, 0x4, 0x1, buf1, 0x1, buf2);
1080  aptCloseSession();
1081 
1082  aptOpenSession();
1084  aptCloseSession();
1085 
1086  aptOpenSession();
1087  ret=APT_PrepareToStartLibraryApplet(NULL, appID);
1088  aptCloseSession();
1089  if(ret!=0)return ret;
1090 
1091  memset(buf1, 0, 4);
1092  aptOpenSession();
1093  APT_AppletUtility(NULL, NULL, 0x4, 0x1, buf1, 0x1, buf2);
1094  aptCloseSession();
1095 
1096  while(1)
1097  {
1098  aptOpenSession();
1099  ret=APT_IsRegistered(NULL, appID, &tmp);
1100  aptCloseSession();
1101  if(ret!=0)return ret;
1102 
1103  if(tmp!=0)break;
1104  }
1105 
1106  // Set status to SUSPENDED.
1109 
1110  // Save Vram
1111  GSPGPU_SaveVramSysArea(NULL);
1112 
1113  // Capture screen.
1114  memset(__ns_capinfo, 0, 0x20);
1115 
1116  aptInitCaptureInfo(__ns_capinfo);
1117 
1118  // Send capture-screen info to the library applet.
1119  aptOpenSession();
1120  APT_SendParameter(NULL, currentAppId, appID, 0x20, __ns_capinfo, 0x0, 0x2);
1121  aptCloseSession();
1122 
1123  // Release GSP module.
1124  GSPGPU_ReleaseRight(NULL);
1125 
1126  __apt_launchapplet_appID = appID;
1127  __apt_launchapplet_inhandle = inhandle;
1128  __apt_launchapplet_parambuf = parambuf;
1129  __apt_launchapplet_parambufsize = parambufsize;
1130 
1131  return 0;
1132 }
1133 
void aptSignalReadyForSleep()
Definition: apt.c:584
Result APT_SetAppCpuTimeLimit(Handle *handle, u32 percent)
Definition: apt.c:900
Result APT_GlanceParameter(Handle *handle, NS_APPID appID, u32 bufferSize, u32 *buffer, u32 *actualSize, u8 *signalType)
Definition: apt.c:757
Result APT_PrepareToStartLibraryApplet(Handle *handle, NS_APPID appID)
Definition: apt.c:1032
Handle aptSleepSync
Definition: apt.c:35
s32 svcClearEvent(Handle handle)
void aptSetStatus(APP_STATUS status)
Definition: apt.c:539
s32 Result
Definition: types.h:42
Result APT_LaunchLibraryApplet(NS_APPID appID, Handle inhandle, u32 *parambuf, u32 parambufsize)
Definition: apt.c:1065
s32 svcCloseHandle(Handle handle)
s32 svcWaitSynchronization(Handle handle, s64 nanoseconds)
Result APT_Enable(Handle *handle, u32 a)
Definition: apt.c:633
Result APT_DoAppJump(Handle *handle, u32 NSbuf0Size, u32 NSbuf1Size, u8 *NSbuf0Ptr, u8 *NSbuf1Ptr)
Definition: apt.c:1013
s32 svcCreateThread(Handle *thread, ThreadFunc entrypoint, u32 arg, u32 *stack_top, s32 thread_priority, s32 processor_id)
void aptCloseSession()
Definition: apt.c:578
uint16_t u16
Definition: types.h:22
Result srvGetServiceHandle(Handle *out, const char *name)
Definition: srv.c:109
Result APT_GetAppCpuTimeLimit(Handle *handle, u32 *percent)
Definition: apt.c:915
Result APT_PrepareToDoAppJump(Handle *handle, u8 flags, u64 programID, u8 mediatype)
Definition: apt.c:996
Result APT_AppletUtility(Handle *handle, u32 *out, u32 a, u32 size1, u8 *buf1, u32 size2, u8 *buf2)
Definition: apt.c:735
Result APT_ReplySleepQuery(Handle *handle, NS_APPID appID, u32 a)
Definition: apt.c:838
Result GSPGPU_SaveVramSysArea(Handle *handle)
Definition: gsp.c:327
void aptAppletStarted()
Definition: apt.c:221
Handle aptEventHandlerThread
Definition: apt.c:27
Result GSPGPU_AcquireRight(Handle *handle, u8 flags)
Definition: gsp.c:278
u32 Handle
Definition: types.h:41
APP_STATUS aptStatusBeforeSleep
Definition: apt.c:33
Result APT_GetLockHandle(Handle *handle, u16 flags, Handle *lockHandle)
Definition: apt.c:589
Result APT_CheckNew3DS_System(Handle *handle, u8 *out)
Definition: apt.c:950
#define U64_MAX
Definition: types.h:12
u32 * getThreadCommandBuffer(void)
Handle aptuHandle
Definition: apt.c:24
uint8_t u8
Definition: types.h:21
Result APT_SendCaptureBufferInfo(Handle *handle, u32 bufferSize, u32 *buffer)
Definition: apt.c:821
#define APT_HANDLER_STACKSIZE
Definition: apt.c:9
void aptInitCaptureInfo(u32 *ns_capinfo)
Definition: apt.c:71
Result APT_HardwareResetAsync(Handle *handle)
Definition: apt.c:621
uint64_t u64
Definition: types.h:24
bool aptMainLoop()
Definition: apt.c:467
void aptAppletUtility_Exit_RetToApp()
Definition: apt.c:106
Result APT_ReplySleepNotificationComplete(Handle *handle, NS_APPID appID)
Definition: apt.c:853
Result APT_JumpToHomeMenu(Handle *handle, u32 a, u32 b, u32 c)
Definition: apt.c:706
uint32_t u32
Definition: types.h:23
void aptReturnToMenu()
Definition: apt.c:148
#define RUNFLAG_APTWORKAROUND
Definition: apt.h:4
Result APT_ReceiveParameter(Handle *handle, NS_APPID appID, u32 bufferSize, u32 *buffer, u32 *actualSize, u8 *signalType)
Definition: apt.c:777
void aptEventHandler(u32 arg)
Definition: apt.c:369
Result APT_SendParameter(Handle *handle, NS_APPID src_appID, NS_APPID dst_appID, u32 bufferSize, u32 *buffer, Handle paramhandle, u8 signalType)
Definition: apt.c:797
u64 aptEventHandlerStack[APT_HANDLER_STACKSIZE/8]
Definition: apt.c:28
Result APT_PrepareToJumpToHomeMenu(Handle *handle)
Definition: apt.c:694
Result APT_GetAppletManInfo(Handle *handle, u8 inval, u8 *outval8, u32 *outval32, NS_APPID *menu_appid, NS_APPID *active_appid)
Definition: apt.c:646
int32_t s32
Definition: types.h:28
Result GSPGPU_RestoreVramSysArea(Handle *handle)
Definition: gsp.c:340
Result APT_InquireNotification(Handle *handle, u32 appID, u8 *signalType)
Definition: apt.c:679
Result GSPGPU_ReleaseRight(Handle *handle)
Definition: gsp.c:294
void aptAppletClosed()
Definition: apt.c:258
u32 aptGetStatusPower()
Definition: apt.c:554
u32 aptParameters[0x1000/4]
Definition: apt.c:37
NS_APPID currentAppId
Definition: apt.c:15
APP_STATUS aptStatus
Definition: apt.c:32
u32 __system_runflags
u32 aptStatusPower
Definition: apt.c:34
Result APT_CheckNew3DS_Application(Handle *handle, u8 *out)
Definition: apt.c:931
Result APT_Initialize(Handle *handle, NS_APPID appId, Handle *eventHandle1, Handle *eventHandle2)
Definition: apt.c:604
Result APT_IsRegistered(Handle *handle, NS_APPID appID, u8 *out)
Definition: apt.c:664
void aptSetStatusPower(u32 status)
Definition: apt.c:563
void aptExit()
Definition: apt.c:437
Result APT_NotifyToWait(Handle *handle, NS_APPID appID)
Definition: apt.c:722
s32 svcCreateEvent(Handle *event, u8 reset_type)
Handle aptStatusMutex
Definition: apt.c:30
Result APT_CloseApplication(Handle *handle, u32 a, u32 b, u32 c)
Definition: apt.c:881
NS_APPID
Definition: apt.h:6
Result aptInit(void)
Definition: apt.c:399
s32 svcCreateMutex(Handle *mutex, bool initially_locked)
APP_STATUS
Definition: apt.h:13
s32 svcSendSyncRequest(Handle session)
void aptWaitStatusEvent()
Definition: apt.c:100
u32 __apt_appid
Result APT_PrepareToCloseApplication(Handle *handle, u8 a)
Definition: apt.c:867
Result APT_StartLibraryApplet(Handle *handle, NS_APPID appID, Handle inhandle, u32 *parambuf, u32 parambufsize)
Definition: apt.c:1046
Result GSPGPU_ImportDisplayCaptureInfo(Handle *handle, GSP_CaptureInfo *captureinfo)
Definition: gsp.c:307
Handle aptEvents[3]
Definition: apt.c:25
NS_APPID aptGetMenuAppID()
Definition: apt.c:137
s32 svcReleaseMutex(Handle handle)
s32 svcSignalEvent(Handle handle)
Handle aptStatusEvent
Definition: apt.c:31
void aptOpenSession()
Definition: apt.c:570
Result GSPGPU_SetLcdForceBlack(Handle *handle, u8 flags)
Definition: gsp.c:219
s32 svcWaitSynchronizationN(s32 *out, Handle *handles, s32 handles_num, bool wait_all, s64 nanoseconds)
GSP_CaptureInfoEntry screencapture[2]
Definition: gsp.h:35
APP_STATUS aptGetStatus()
Definition: apt.c:530
Handle aptLockHandle
Definition: apt.c:23
Result APT_CheckNew3DS(Handle *handle, u8 *out)
Definition: apt.c:969