system-controller: fixes for multi display setups
[profile/ivi/murphy.git] / packaging.in / murphy.lua
1 with_system_controller = false
2 with_amb = false
3 verbose = 0
4
5 m = murphy.get()
6
7 -- try loading the various logging plugins
8 m:try_load_plugin('systemd')
9 m:try_load_plugin('dlog')
10
11 -- load the console plugin
12 m:try_load_plugin('console')
13
14 m:try_load_plugin('console.disabled', 'webconsole', {
15                   address = 'wsck:127.0.0.1:3000/murphy',
16                   httpdir = '/usr/share/murphy/webconsole' });
17
18 -- load the dbus plugin
19 if m:plugin_exists('dbus') then
20     m:load_plugin('dbus')
21 end
22
23 -- load the native resource plugin
24 if m:plugin_exists('resource-native') then
25     m:load_plugin('resource-native')
26     m:info("native resource plugin loaded")
27 else
28     m:info("No native resource plugin found...")
29 end
30
31 -- load the dbus resource plugin
32 m:try_load_plugin('resource-dbus', {
33     dbus_bus = "system",
34     dbus_service = "org.Murphy",
35     dbus_track = true,
36     default_zone = "driver",
37     default_class = "implicit"
38 })
39
40 -- load the domain control plugin
41 if m:plugin_exists('domain-control') then
42     m:load_plugin('domain-control')
43 else
44     m:info("No domain-control plugin found...")
45 end
46
47 if m:plugin_exists('glib') then
48     m:load_plugin('glib')
49 else
50     m:info("No glib plugin found...")
51 end
52
53 -- load the AMB plugin
54 if m:plugin_exists('amb') then
55     m:try_load_plugin('amb')
56
57     if builtin.method.amb_initiate and
58        builtin.method.amb_update
59     then
60         with_amb = true
61     end
62 else
63     m:info("No amb plugin found...")
64 end
65
66 -- load the ASM resource plugin
67 if m:plugin_exists('resource-asm') then
68     m:try_load_plugin('resource-asm', {
69         zone = "driver",
70         share_mmplayer = "player:AVP,mandatory,exclusive,strict",
71         ignored_argv0 = "WebProcess"
72     })
73 else
74     m:info("No audio session manager plugin found...")
75 end
76
77 if m:plugin_exists('system-controller') then
78     with_system_controller = true
79 elseif m:plugin_exists('ivi-resource-manager') then
80     m:load_plugin('ivi-resource-manager')
81     with_system_controller = false
82 end
83
84 -- define application classes
85 application_class {
86     name     = "interrupt",
87     priority = 99,
88     modal    = true ,
89     share    = false,
90     order    = "fifo"
91 }
92
93 application_class {
94     name     = "emergency",
95     priority = 80,
96     modal    = false,
97     share    = false,
98     order    = "fifo"
99 }
100 application_class {
101     name     = "system",
102     priority = 52,
103     modal    = false,
104     share    = true,
105     order    = "lifo"
106 }
107 application_class {
108     name     = "alert",
109     priority = 51,
110     modal    = false,
111     share    = false,
112     order    = "fifo"
113 }
114
115 application_class {
116     name     = "navigator",
117     priority = 50,
118     modal    = false,
119     share    = true,
120     order    = "fifo"
121 }
122
123 application_class {
124     name     = "phone",
125     priority = 6 ,
126     modal    = false,
127     share    = true ,
128     order    = "lifo"
129 }
130 application_class {
131     name     = "camera",
132     priority = 5,
133     modal    = false,
134     share    = false,
135     order    = "lifo"
136 }
137
138 application_class { name="event"    , priority=4 , modal=false, share=true , order="fifo" }
139 application_class { name="game"     , priority=3 , modal=false, share=false, order="lifo" }
140 --# doesn't need to be created here, ivi-resource-manager creates it if loaded
141 --#application_class { name="basic"    , priority=2 , modal=false, share=false, order="lifo" }
142 application_class { name="player"   , priority=1 , modal=false, share=true , order="lifo" }
143 application_class { name="implicit" , priority=0 , modal=false, share=false, order="lifo" }
144
145 -- define zone attributes
146 zone.attributes {
147     type = {mdb.string, "common", "rw"},
148     location = {mdb.string, "anywhere", "rw"}
149 }
150
151 -- define zones
152 zone {
153      name = "driver",
154      attributes = {
155          type = "common",
156          location = "front-left"
157      }
158 }
159
160 zone {
161      name = "passanger1",
162      attributes = {
163          type = "private",
164          location = "front-right"
165      }
166 }
167
168 zone {
169      name = "passanger2",
170      attributes = {
171          type = "private",
172          location = "back-left"
173      }
174 }
175
176 zone {
177      name = "passanger3",
178      attributes = {
179          type = "private",
180          location = "back-right"
181      }
182 }
183
184 zone {
185      name = "passanger4",
186      attributes = {
187          type = "private",
188          location = "back-left"
189      }
190 }
191
192
193 -- define resource classes
194 if not m:plugin_exists('ivi-resource-manager') and
195    not with_system_controller
196 then
197    resource.class {
198         name = "audio_playback",
199         shareable = true,
200         attributes = {
201             role = { mdb.string, "music", "rw" },
202             pid = { mdb.string, "<unknown>", "rw" },
203             policy = { mdb.string, "relaxed", "rw" }
204         }
205    }
206 end
207
208 resource.class {
209      name = "audio_recording",
210      shareable = true,
211      attributes = {
212          role   = { mdb.string, "music"    , "rw" },
213          pid    = { mdb.string, "<unknown>", "rw" },
214          policy = { mdb.string, "relaxed"  , "rw" }
215      }
216 }
217
218 resource.class {
219      name = "video_playback",
220      shareable = false,
221 }
222
223 resource.class {
224      name = "video_recording",
225      shareable = false
226 }
227
228 resource.class {
229      name = "speech_recognition",
230      shareable = true
231 }
232
233 resource.class {
234      name = "speech_synthesis",
235      shareable = true
236 }
237
238 if not m:plugin_exists('ivi-resource-manager') and
239    not with_system_controller
240 then
241     resource.method.veto = {
242         function(zone, rset, grant, owners, req_set)
243             return true
244          end
245     }
246 end
247
248 -- test for creating selections
249 mdb.select {
250            name = "audio_owner",
251            table = "audio_playback_owner",
252            columns = {"application_class"},
253            condition = "zone_name = 'driver'"
254 }
255
256 mdb.select {
257            name = "vehicle_speed",
258            table = "amb_vehicle_speed",
259            columns = {"value"},
260            condition = "key = 'VehicleSpeed'"
261 }
262
263 element.lua {
264    name    = "speed2volume",
265    inputs  = { speed = mdb.select.vehicle_speed, param = 9 },
266    outputs = {  mdb.table { name = "speedvol",
267                             index = {"zone", "device"},
268                             columns = {{"zone", mdb.string, 16},
269                                        {"device", mdb.string, 16},
270                                        {"value", mdb.floating}},
271                             create = true
272                            }
273              },
274    oldvolume = 0.0,
275    update  = function(self)
276                 speed = self.inputs.speed.single_value
277                 if (speed) then
278                     volume = (speed - 144.0) / 7.0
279                 else
280                     volume = 0.0
281                 end
282                 diff = volume - self.oldvolume
283                 if (diff*diff > self.inputs.param) then
284                     print("*** element "..self.name.." update "..volume)
285                     self.oldvolume = volume
286                     mdb.table.speedvol:replace({zone = "driver", device = "speakers", value = volume})
287                 end
288              end
289 }
290
291 mdb.select {
292     name = "amb_state",
293     table = "amb_state",
294     columns = { "state" },
295     condition = "id = 0"
296 }
297
298 -- Night mode processing chain
299
300 mdb.select {
301     name = "exterior_brightness",
302     table = "amb_exterior_brightness",
303     columns = { "value" },
304     condition = "key = 'ExteriorBrightness'"
305 }
306
307 element.lua {
308     name    = "nightmode",
309     inputs  = { brightness = mdb.select.exterior_brightness },
310     oldmode = -1;
311     outputs = {
312         mdb.table {
313             name = "amb_nightmode",
314             index = { "id" },
315             create = true,
316             columns = {
317                 { "id", mdb.unsigned },
318                 { "night_mode", mdb.unsigned }
319             }
320         }
321     },
322     update = function(self)
323         -- This is a trivial function to calculate night mode. Later, we will
324         -- need a better threshold value and hysteresis to prevent oscillation.
325
326         brightness = self.inputs.brightness.single_value
327
328         if not brightness then
329             return
330         end
331
332         print("*** element "..self.name.." update brightness: "..brightness)
333
334         if brightness > 300 then
335             mode = 0
336         else
337             mode = 1
338         end
339
340         print("*** resulting mode: ".. mode)
341
342         if not (mode == self.oldmode) then
343             mdb.table.amb_nightmode:replace({ id = 0, night_mode = mode })
344         end
345
346         self.oldmode = mode
347     end
348 }
349
350 mdb.select {
351     name = "select_night_mode",
352     table = "amb_nightmode",
353     columns = { "night_mode" },
354     condition = "id = 0"
355 }
356
357 if with_amb then
358     sink.lua {
359         name = "night_mode",
360         inputs = { NightMode = mdb.select.select_night_mode,
361                    amb_state = mdb.select.amb_state },
362         property = "NightMode",
363         type = "b",
364         initiate = builtin.method.amb_initiate,
365         update = builtin.method.amb_update
366     }
367 end
368
369 -- Night mode general handlers
370
371 sink.lua {
372     name = "nightmode_homescreen",
373     inputs = { owner = mdb.select.select_night_mode },
374     initiate = function(self)
375         -- data = mdb.select.select_night_mode.single_value
376         return true
377     end,
378     update = function(self)
379         data = mdb.select.select_night_mode.single_value
380
381         if verbose > 1 then
382             print("Night mode updated: " .. tostring(data))
383         end
384
385         if sc then
386             -- tell homescreen that night mode was updated
387             sc:send_message(homescreen, m:JSON({command=0x60001,arg=m:JSON({stateid=2,state=data})}))
388         end
389         return true
390     end
391 }
392
393 -- Driving mode processing chain
394
395 element.lua {
396     name    = "drivingmode",
397     inputs  = { speed = mdb.select.vehicle_speed },
398     oldmode = -1;
399     outputs = {
400         mdb.table {
401             name = "amb_drivingmode",
402             index = { "id" },
403             create = true,
404             columns = {
405                 { "id", mdb.unsigned },
406                 { "driving_mode", mdb.unsigned }
407             }
408         }
409     },
410     update = function(self)
411
412         speed = self.inputs.speed.single_value
413
414         if not speed then
415             return
416         end
417
418         if speed == 0 then
419             mode = 0
420         else
421             mode = 1
422         end
423
424         if not (mode == self.oldmode) then
425             mdb.table.amb_drivingmode:replace({ id = 0, driving_mode = mode })
426         end
427
428         self.oldmode = mode
429     end
430 }
431
432 mdb.select {
433     name = "select_driving_mode",
434     table = "amb_drivingmode",
435     columns = { "driving_mode" },
436     condition = "id = 0"
437 }
438
439 if with_amb then
440     sink.lua {
441         name = "driving_mode",
442         inputs = { DrivingMode = mdb.select.select_driving_mode,
443                    amb_state = mdb.select.amb_state },
444         property = "DrivingMode",
445         type = "u",
446         initiate = builtin.method.amb_initiate,
447         update = builtin.method.amb_update
448     }
449 end
450
451 -- turn signals (left, right)
452
453 mdb.select {
454     name = "winker",
455     table = "amb_turn_signal",
456     columns = { "value" },
457     condition = "key = 'TurnSignal'"
458 }
459
460 -- define three categories
461
462 mdb.select {
463     name = "undefined_applications",
464     table = "aul_applications",
465     columns = { "appid" },
466     condition = "category = '<undefined>'"
467 }
468
469 mdb.select {
470     name = "basic_applications",
471     table = "aul_applications",
472     columns = { "appid" },
473     condition = "category = 'basic'"
474 }
475
476 mdb.select {
477     name = "entertainment_applications",
478     table = "aul_applications",
479     columns = { "appid" },
480     condition = "category = 'entertainment'"
481 }
482
483 function ft(t)
484     -- filter the object garbage out of the tables
485     ret = {}
486
487     for k,v in pairs(t) do
488         if k ~= "userdata" and k ~= "new" then
489             ret[k] = v
490         end
491     end
492
493     return ret
494 end
495
496 function getApplication(appid)
497     local conf = nil
498
499     -- find the correct local application definition
500
501     for k,v in pairs(ft(application)) do
502         if appid == v.appid then
503             conf = v
504             break
505         end
506     end
507
508     return conf
509 end
510
511 function regulateApplications(t, regulation)
512     for k,v in pairs(ft(t)) do
513
514         -- iterate through the undefined and entertainment apps, see if
515         -- they have been overruled in local config
516
517         local conf = getApplication(v.appid)
518
519         if conf and conf.resource_class ~= "player" then
520             -- override, don't disable
521             resmgr:disable_screen_by_appid("*", "*", v.appid, false)
522         else
523             resmgr:disable_screen_by_appid("*", "*", v.appid, regulation == 1)
524         end
525     end
526 end
527
528 -- regulation (on), use "select_driving_mode"
529
530 sink.lua {
531     name = "driving_regulation",
532     inputs = { owner = mdb.select.select_driving_mode },
533     initiate = function(self)
534         -- local data = mdb.select.select_driving_mode.single_value
535         return true
536     end,
537     update = function(self)
538         local data = mdb.select.select_driving_mode.single_value
539
540         if verbose > 1 then
541             print("Driving mode updated: " .. tostring(data))
542         end
543
544         if not sc then
545             return true
546         end
547
548         -- tell homescreen that driving mode was updated
549         sc:send_message(homescreen, m:JSON({command=0x60001,arg=m:JSON({stateid=1,state=data})}))
550
551         --[[
552         -- bulk handle the applications that need requisites
553         r = requisite { driving = true }
554         resmgr:disable_screen_by_requisite("*", "*", r, data == 1)
555         --]]
556
557         regulateApplications(ft(mdb.select.entertainment_applications), data)
558         regulateApplications(ft(mdb.select.undefined_applications), data)
559
560         return true
561     end
562 }
563 --[[
564 sink.lua {
565     name = "regulated_app_change",
566     inputs = { undef = mdb.select.undefined_applications,
567                entertainment = mdb.select.entertainment_applications },
568     initiate = function(self)
569         return true
570     end,
571     update = function(self)
572         local data = mdb.select.select_driving_mode.single_value
573
574         if not sc then
575             return
576         end
577
578         if verbose > 1 then
579             print("regulated application list was changed")
580         end
581
582         regulateApplications(ft(mdb.select.entertainment_applications), data)
583         regulateApplications(ft(mdb.select.undefined_applications), data)
584
585         return true
586     end
587 }
588 --]]
589
590 -- shift position (parking, reverse, other)
591
592 mdb.select {
593     name = "gear_position",
594     table = "amb_gear_position",
595     columns = { "value" },
596     condition = "key = 'GearPosition'"
597 }
598
599 -- cameras (back, front, left, right)
600
601 element.lua {
602     name    = "camera_state",
603     inputs  = { winker = mdb.select.winker, gear = mdb.select.gear_position },
604     oldmode = -1;
605     outputs = {
606         mdb.table {
607             name = "target_camera_state",
608             index = { "id" },
609             create = true,
610             columns = {
611                 { "id", mdb.unsigned },
612                 { "front_camera", mdb.unsigned },
613                 { "back_camera", mdb.unsigned },
614                 { "right_camera", mdb.unsigned },
615                 { "left_camera", mdb.unsigned }
616             }
617         }
618     },
619     update = function(self)
620
621         front_camera = 0
622         back_camera = 0
623         right_camera = 0
624         left_camera = 0
625
626         if self.inputs.gear == 128 then
627             back_camera = 1
628         elseif self.inputs.winker == 1 then
629             right_camera = 1
630         elseif self.inputs.winker == 2 then
631             left_camera = 1
632         end
633
634         mdb.table.target_camera_state:replace({ id = 0, front_camera = front_camera, back_camera = back_camera, right_camera = right_camera, left_camera = left_camera })
635
636     end
637 }
638
639 -- load the telephony plugin
640 m:try_load_plugin('telephony')
641
642
643 -- system controller test setup
644
645 if not with_system_controller then
646    return
647 end
648
649 m:load_plugin('system-controller')
650
651 window_manager_operation_names = {
652     [1] = "create",
653     [2] = "destroy"
654 }
655
656 function window_manager_operation_name(oper)
657     local name = window_manager_operation_names[oper]
658     if name then return name end
659     return "<unknown " .. tostring(oper) .. ">"
660 end
661
662 window_operation_names = {
663     [1] = "create",
664     [2] = "destroy",
665     [3] = "name_change",
666     [4] = "visible",
667     [5] = "configure",
668     [6] = "active"
669 }
670
671 function window_operation_name(oper)
672     local name = window_operation_names[oper]
673     if name then return name end
674     return "<unknown " .. tostring(oper) .. ">"
675 end
676
677 layer_operation_names = {
678     [1] = "create",
679     [2] = "destroy",
680     [3] = "visible"
681 }
682
683 function layer_operation_name(oper)
684     local name = layer_operation_names[oper]
685     if name then return name end
686     return "<unknown " .. tostring(oper) .. ">"
687 end
688
689 input_manager_operation_names = {
690     [1] = "create",
691     [2] = "destroy",
692     [3] = "ready"
693 }
694
695 function input_manager_operation_name(oper)
696     local name = input_manager_operation_names[oper]
697     if name then return name end
698     return "<unknown " .. tostring(oper) .. ">"
699 end
700
701 input_operation_names = {
702     [1] = "create",
703     [2] = "destroy",
704     [3] = "update"
705 }
706
707 function input_operation_name(oper)
708     local name = input_operation_names[oper]
709     if name then return name end
710     return "<unknown " .. tostring(oper) .. ">"
711 end
712
713 code_operation_names = {
714     [1] = "create",
715     [2] = "destroy",
716     [3] = "state_change"
717 }
718
719 function code_operation_name(oper)
720     local name = code_operation_names[oper]
721     if name then return name end
722     return "<unknown " .. tostring(oper) .. ">"
723 end
724
725 command_names = {
726     [0x00001] = "send_appid",
727     [0x10001] = "create",
728     [0x10002] = "destroy",
729     [0x10003] = "show",
730     [0x10004] = "hide",
731     [0x10005] = "move",
732     [0x10006] = "change_active",
733     [0x10007] = "change_layer",
734     [0x10008] = "change_attr",
735     [0x10009] = "name",
736     [0x10011] = "map_thumb",
737     [0x10012] = "unmap_thumb",
738     [0x10020] = "show layer",
739     [0x10021] = "hide_layer",
740     [0x10022] = "change_layer_attr",
741     [0x20001] = "add_input",
742     [0x20002] = "del_input",
743     [0x20003] = "send_input",
744     [0x40001] = "acquire_res",
745     [0x40002] = "release_res",
746     [0x40003] = "deprive_res",
747     [0x40004] = "waiting_res",
748     [0x40005] = "revert_res",
749     [0x40011] = "create_res",
750     [0x40012] = "destroy_res",
751     [0x50001] = "set_region",
752     [0x50002] = "unset_region",
753     [0x60001] = "change_state"
754 }
755
756 function command_name(command)
757     local name = command_names[command]
758     if name then return name end
759     return "<unknown " .. tostring(command) .. ">"
760 end
761
762 input_layer = {
763    [3] = true, -- input
764    [4] = true, -- touch
765    [5] = true  -- cursor
766 }
767
768 resmgr = resource_manager {
769   screen_event_handler = function(self, ev)
770                              local event = ev.event
771                              local surface = ev.surface
772
773                              if event == "grant" then
774                                  if verbose > 0 then
775                                     print("*** make visible surface "..surface)
776                                  end
777                                  local a = animation({})
778                                  local r = m:JSON({surface = surface,
779                                                    visible = 1,
780                                                    raise   = 1})
781                                  wmgr:window_request(r,a,0)
782                                  elseif event == "revoke" then
783                                  if verbose > 0 then
784                                     print("*** hide surface "..surface)
785                                  end
786                                  local a = animation({})
787                                  local r = m:JSON({surface = ev.surface,
788                                                    visible = 0})
789                                  wmgr:window_request(r,a,0)
790
791                              elseif event == "create" then
792
793                                 if verbose > 0 then
794                                     print("*** screen resource event: " ..
795                                           tostring(ev))
796                                 end
797
798                                 local regulation = mdb.select.select_driving_mode.single_value
799
800                                 if regulation == 1 then
801
802                                     local blacklisted = false
803
804                                     -- applications which have their category set to "entertainment"
805                                     -- or "undefined" are blacklisted, meaning they should be regulated
806
807                                     for i,v in pairs(ft(mdb.select.undefined_applications)) do
808                                         if v.appid == ev.appid then
809                                             if verbose > 0 then
810                                                 print(ev.appid .. " was blacklisted (undefined)")
811                                             end
812                                             blacklisted = true
813                                             break
814                                         end
815                                     end
816
817                                     if not blacklisted then
818                                         for i,v in pairs(ft(mdb.select.entertainment_applications)) do
819                                             if v.appid == ev.appid then
820                                                 if verbose > 0 then
821                                                     print(ev.appid .. " was blacklisted (entertainment)")
822                                                 end
823                                                 blacklisted = true
824                                                 break
825                                             end
826                                         end
827                                     end
828
829                                     -- our local application config, which takes precedence
830                                     local conf = getApplication(ev.appid)
831
832                                     -- disable only non-whitelisted applications
833                                     if not conf or conf.resource_class == "player" then
834                                         if blacklisted then
835                                             if verbose > 0 then
836                                                 print("disabling screen for " .. ev.appid)
837                                             end
838                                             resmgr:disable_screen_by_appid("*", "*", ev.appid, true)
839                                         end
840                                     end
841                                 end
842
843                              elseif event == "destroy" then
844                                if verbose > 0 then
845                                     print("*** screen resource event: " ..
846                                           tostring(ev))
847                                  end
848                              else
849                                  if verbose > 0 then
850                                     print("*** screen resource event: " ..
851                                           tostring(ev))
852                                  end
853                              end
854                          end,
855   audio_event_handler = function(self, ev)
856                              local event = ev.event
857                              local appid = ev.appid
858                              local audioid = ev.audioid
859
860                              if event == "grant" then
861                                  if verbose > 0 or true then
862                                     print("*** grant audio to "..appid..
863                                           " ("..audioid..") in '" ..
864                                           ev.zone .. "' zone")
865                                  end
866                              elseif event == "revoke" then
867                                  if verbose > 0 or true then
868                                     print("*** revoke audio from "..appid..
869                                           " ("..audioid..") in '" ..
870                                           ev.zone .. "' zone")
871                                  end
872                              else
873                                  if verbose > 0 or true then
874                                     print("*** audio resource event: " ..
875                                           tostring(ev))
876                                  end
877                              end
878                         end
879 }
880
881 resclnt = resource_client {}
882
883 wmgr = window_manager {
884   geometry = function(self, w,h, v)
885                   if type(v) == "function" then
886                       return v(w,h)
887                   end
888                   return v
889              end,
890
891   application = function(self, appid)
892                      if appid then
893                          local app = application_lookup(appid)
894                          if not app then
895                              app = application_lookup("default")
896                          end
897                          return app
898                      end
899                      return { privileges = {screen="none", audio="none"} }
900                 end,
901
902   output_order = { 1, 0 },
903
904   outputs = { { name  = "Mid",
905                 id    = 0,
906                 zone  = "driver",
907                 areas = { Full = {
908                               id     = 20,
909                               pos_x  = 0,
910                               pos_y  = 0,
911                               width  = function(w,h) return w end,
912                               height = function(w,h) return h end
913                           },
914                           Left = {
915                               id     = 21,
916                               pos_x  = 0,
917                               pos_y  = 0,
918                               width  = 320,
919                               height = function(w,h) return h end
920                           },
921                           Right = {
922                               id     = 22,
923                               pos_x  = function(w,h) return w-320 end,
924                               pos_y  = 0,
925                               width  = 320,
926                               height = function(w,h) return h end
927                           }
928                         }
929                },
930                { name  = "Center",
931                  id    = 1,
932                  zone  = "driver",
933                  areas = { Status = {
934                               id     = 0,
935                               pos_x  = 0,
936                               pos_y  = 0,
937                               width  = function(w,h) return w end,
938                               height = 64
939                            },
940                            Full = {
941                               id     = 1,
942                               pos_x  = 0,
943                               pos_y  = 64,
944                               width  = function(w,h) return w end,
945                               height = function(w,h) return h-64-128 end
946                            },
947                            Upper = {
948                               id     = 2,
949                               pos_x  = 0,
950                               pos_y  = 64,
951                               width  = function(w,h) return w end,
952                               height = function(w,h) return (h-64-128)/2 end
953                            },
954                            Lower = {
955                               id     = 3,
956                               pos_x  = 0,
957                               pos_y  = function(w,h) return (h-64-128)/2+64 end,
958                               width  = function(w,h) return w end,
959                               height = function(w,h) return (h-64-128)/2 end
960                            },
961                            UpperLeft = {
962                               id     = 4,
963                               pos_x  = 0,
964                               pos_y  = 64,
965                               width  = function(w,h) return w/2 end,
966                               height = function(w,h) return (h-64-128)/2 end
967                            },
968                            UpperRight = {
969                               id     = 5,
970                               pos_x  = function(w,h) return w/2 end,
971                               pos_y  = 64,
972                               width  = function(w,h) return w/2 end,
973                               height = function(w,h) return (h-64-128)/2 end
974                            },
975                            LowerLeft = {
976                               id     = 6,
977                               pos_x  = 0,
978                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
979                               width  = function(w,h) return w/2 end,
980                               height = function(w,h) return (h-64-128)/2 end
981                            },
982                            LowerRight = {
983                               id     = 7,
984                               pos_x  = function(w,h) return w/2 end,
985                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
986                               width  = function(w,h) return w/2 end,
987                               height = function(w,h) return (h-64-128)/2 end
988                            },
989                            SysApp = {
990                               id     = 8,
991                               pos_x  = 0,
992                               pos_y  = 64,
993                               width  = function(w,h) return w end,
994                               height = function(w,h) return h-64-128 end
995                            },
996                            ["SysApp.Left"] = {
997                               id     = 9,
998                               pos_x  = 0,
999                               pos_y  = 64,
1000                               width  = function(w,h) return w/2-181 end,
1001                               height = function(w,h) return h-64-128 end
1002                            },
1003                            ["SysApp.Right"] = {
1004                               id     = 10,
1005                               pos_x  = function(w,h) return w/2+181 end,
1006                               pos_y  = 64,
1007                               width  = function(w,h) return w/2-181 end,
1008                               height = function(w,h) return h-64-128 end
1009                            },
1010                         }
1011               }
1012   },
1013   layers = { {      0, "Background"   , 1 },
1014              {      1, "Application"  , 2 },
1015              {      2, "Softkeyboard" , 4 },
1016              {      3, "HomeScreen"   , 2 },
1017              {      4, "ControlBar"   , 2 },
1018              {      5, "InterruptApp" , 2 },
1019              {      6, "OnScreen"     , 2 },
1020              {    101, "Input"        , 3 },
1021              {    102, "Cursor"       , 5 },
1022              {    103, "Startup"      , 6 },
1023              { 0x1000, "Background"   , 1 },
1024              { 0x2000, "Normal"       , 2 },
1025              { 0x3000, "Fullscreen"   , 2 },
1026              { 0x4000, "InputPanel"   , 3 },
1027              { 0xA000, "Touch"        , 4 },
1028              { 0xB000, "Cursor"       , 5 },
1029              { 0xC000, "Startup"      , 6 }
1030   },
1031
1032
1033   manager_update = function(self, oper)
1034                        if verbose > 0 then
1035                            print("### <== WINDOW MANAGER UPDATE:" ..
1036                                  window_manager_operation_name(oper))
1037                        end
1038                        if oper == 1 then
1039                            local umask = window_mask { raise   = true,
1040                                                        visible = true,
1041                                                        active  = true }
1042                            local rmask = window_mask { active  = true }
1043                            local req = m:JSON({
1044                                        passthrough_update  = umask:tointeger(),
1045                                        passthrough_request = rmask:tointeger()
1046                            })
1047                            self:manager_request(req)
1048                        end
1049                    end,
1050
1051   window_update = function(self, oper, win, mask)
1052                       if verbose > 0 then
1053                           print("### <== WINDOW UPDATE oper:" ..
1054                                 window_operation_name(oper) ..
1055                                 " mask: " .. tostring(mask))
1056                           if verbose > 1 then
1057                               print(win)
1058                           end
1059                       end
1060
1061                       local arg = m:JSON({ surface = win.surface,
1062                                            winname = win.name,
1063                       })
1064                       local command = 0
1065
1066                       if oper == 1 then -- create
1067                            local layertype = win.layertype
1068                            if layertype and input_layer[layertype] then
1069                                if verbose > 0 then
1070                                    print("ignoring input panel creation")
1071                                end
1072                                return
1073                            end
1074                            command     = 0x10001
1075                       elseif oper == 2 then -- destroy
1076                            command     = 0x10002
1077                       elseif oper == 3 then  -- namechange
1078                            command     = 0x10009
1079                       elseif oper == 4 or oper == 5 then --visible or configure
1080                            command     = 0x10008
1081                            arg.zone    = win.area
1082                            arg.node    = win.node
1083                            arg.layer   = win.layer
1084                            arg.pos_x   = win.pos_x
1085                            arg.pos_y   = win.pos_y
1086                            arg.width   = win.width
1087                            arg.height  = win.height
1088                            arg.raise   = win.raise
1089                            arg.visible = win.visible
1090                            arg.active  = win.active
1091                       elseif oper == 6 then -- active
1092                            command     = 0x10006
1093                            arg.active  = win.active
1094                       else
1095                            if verbose > 0 then
1096                                print("### nothing to do")
1097                            end
1098                            return
1099                       end
1100
1101                       local msg = m:JSON({ command = command,
1102                                            appid   = win.appid,
1103                                            pid     = win.pid,
1104                                            arg     = arg
1105                       })
1106                       if verbose > 0 then
1107                           print("### <== sending " ..
1108                                 command_name(msg.command) ..
1109                                 " window message to '" .. win.name .. "'")
1110                           if verbose > 1 then
1111                               print(msg)
1112                           end
1113                       end
1114                       sc:send_message(homescreen, msg)
1115
1116                       if oper == 1 then -- create
1117                           local i = input_layer[win.layertype]
1118                           local p = self:application(win.appid)
1119                           local s = p.privileges.screen
1120
1121                           if s == "system" then
1122                               local a = animation({})
1123                               local r = m:JSON({surface = win.surface,
1124                                                 visible = 1,
1125                                                 raise   = 1})
1126                               self:window_request(r,a,0)
1127                           else
1128                               if i then
1129                                   if verbose > 0 then
1130                                       print("do not make resource for " ..
1131                                             "input window")
1132                                   end
1133                               else
1134                                   resclnt:resource_set_create("screen",
1135                                                                "driver",
1136                                                                win.appid,
1137                                                                win.surface)
1138                               end
1139                           end
1140                       elseif oper == 2 then -- destroy
1141                           resclnt:resource_set_destroy("screen", win.surface)
1142                       elseif oper == 6 then -- active
1143                           if win.active then
1144                               local i = input_layer[win.layertype]
1145                               local p = self:application(win.appid)
1146                               local s = p.privileges.screen
1147                               local surface = win.surface
1148                               if not i and s ~= "system" then
1149                                  resclnt:resource_set_acquire("screen",surface)
1150                                  resmgr:window_raise(win.appid, surface, 1)
1151                               end
1152                           end
1153                       end
1154                   end,
1155
1156   layer_update = function(self, oper, layer, mask)
1157                       if verbose > 0 then
1158                           print("### LAYER UPDATE:" ..
1159                                 layer_operation_name(oper) ..
1160                                 " mask: " .. tostring(mask))
1161                           if verbose > 1 then
1162                               print(layer)
1163                           end
1164                       end
1165                       if oper == 3 then -- visible
1166                          local command = 0x10022
1167                          local msg = m:JSON({
1168                                          command = command,
1169                                          appid = "",
1170                                          arg = m:JSON({layer = layer.id,
1171                                                        visible = layer.visible
1172                                          })
1173                                 })
1174                          if verbose > 0 then
1175                             print("### <== sending "..command_name(command)..
1176                                   " layer message")
1177                             if verbose > 1 then
1178                                 print(msg)
1179                             end
1180                          end
1181                          sc:send_message(homescreen, msg)
1182                       else
1183                            if verbose > 0 then
1184                                print("### nothing to do")
1185                            end
1186                       end
1187                  end,
1188
1189   output_update = function(self, oper, out, mask)
1190                       local idx = out.index
1191                       local defidx = self.output_order[idx+1]
1192                       if verbose > 0 then
1193                           print("### OUTPUT UPDATE:" .. oper ..
1194                                 " mask: "..tostring(mask))
1195                       end
1196                       if not defidx then
1197                           return
1198                       end
1199                       print(out)
1200                       local outdef = self.outputs[defidx+1]
1201                       if (oper == 1) then -- create
1202                           if outdef then
1203                               self:output_request(m:JSON({index = idx,
1204                                                           id    = outdef.id,
1205                                                           name  = outdef.name
1206                                                           }))
1207                           end
1208                       elseif (oper == 5) then -- done
1209                           local ads = outdef.areas
1210                           local on = outdef.name
1211                           if ads then
1212                               for name,ad in pairs(ads) do
1213                                   local can = wmgr:canonical_name(on.."."..name)
1214                                   local a = m:JSON({name   = name,
1215                                                     output = out.index})
1216                                   for fld,val in pairs(ad) do
1217                                       a[fld] = self:geometry(out.width,
1218                                                              out.height,
1219                                                              val)
1220                                    end
1221                                    self:area_create(a)
1222                                    resmgr:area_create(area[can], outdef.zone)
1223                               end
1224                           end
1225                       end
1226                   end
1227 }
1228
1229
1230 imgr = input_manager {
1231   inputs = {{ name = "G27 Racing Wheel",
1232               id = 0,
1233               switch = { [2] = {appid="org.tizen.ico.app-soundsample"      },
1234                          [3] = {appid="org.tizen.ico.homescreen", keycode=1},
1235                          [4] = {appid="org.tizen.ico.app-soundsample"      },
1236                          [5] = {appid="org.tizen.ico.homescreen", keycode=2}
1237              }}
1238   },
1239
1240   manager_update = function(self, oper)
1241                        if verbose > 0 then
1242                            print("### <== INPUT MANAGER UPDATE:" ..
1243                                  input_manager_operation_name(oper))
1244                        end
1245                    end,
1246
1247   input_update = function(self, oper, inp, mask)
1248                      if verbose > 0 then
1249                          print("### INPUT UPDATE:" ..
1250                                 input_operation_name(oper) ..
1251                                 " mask: " .. tostring(mask))
1252                           if verbose > 1 then
1253                               print(inp)
1254                           end
1255                       end
1256                  end,
1257   code_update  = function(self, oper, code, mask)
1258                      if verbose > 0 then
1259                          print("### CODE UPDATE: mask: " .. tostring(mask))
1260                          if verbose > 1 then
1261                              print(code)
1262                          end
1263                      end
1264                      local msg = m:JSON({ command = 1,
1265                                           appid = "org.tizen.ico.homescreen",
1266                                           arg = m:JSON({ device = code.device,
1267                                                          time = code.time,
1268                                                          input = code.input,
1269                                                          code = code.id,
1270                                                          state = code.state
1271                                            })
1272                      })
1273                      if verbose > 0 then
1274                          print("### <== sending " ..
1275                                command_name(msg.command) ..
1276                                " input message")
1277                          if verbose > 1 then
1278                              print(msg)
1279                          end
1280                      end
1281                      sc:send_message(homescreen, msg)
1282                  end
1283 }
1284
1285 sc = m:get_system_controller()
1286
1287 -- resource sets
1288 sets = {}
1289
1290 -- user manager
1291 um = m:UserManager()
1292
1293 connected = false
1294 homescreen = ""
1295
1296 -- these shoud be before wmgr:connect() is called
1297 if verbose > 0 then
1298    print("====== creating applications ======")
1299 end
1300 application {
1301     appid          = "default",
1302     area           = "Center.Full",
1303     privileges     = { screen = "none", audio = "none" },
1304     resource_class = "player",
1305     screen_priority = 0
1306 }
1307
1308 application {
1309     appid           = "weston",
1310     area            = "Center.Full",
1311     privileges      = { screen = "system", audio = "none" },
1312     resource_class  = "implicit",
1313     screen_priority = 30
1314 }
1315
1316 application {
1317     appid           = "org.tizen.ico.homescreen",
1318     area            = "Center.Full",
1319     privileges      = { screen = "system", audio = "system" },
1320     resource_class  = "player",
1321     screen_priority = 20
1322 }
1323
1324 application {
1325     appid           = "org.tizen.ico.statusbar",
1326     area            = "Center.Status",
1327     privileges      = { screen = "system", audio = "none" },
1328     resource_class  = "player",
1329     screen_priority = 20
1330 }
1331
1332 application {
1333     appid           = "org.tizen.ico.login",
1334     area            = "Center.Full",
1335     privileges      = { screen = "system", audio = "system" },
1336     resource_class  = "player",
1337     screen_priority = 20
1338 }
1339
1340 application {
1341     appid           = "org.tizen.ico.camera_left",
1342     area            = "Center.SysApp.Left",
1343     privileges      = { screen = "system", audio = "none" },
1344     requisites      = { screen = "blinker_left", audio = "none" },
1345     resource_class  = "player",
1346     screen_priority = 30
1347 }
1348
1349 application {
1350     appid           = "org.tizen.ico.camera_right",
1351     area            = "Center.SysApp.Right",
1352     privileges      = { screen = "system", audio = "none" },
1353     requisites      = { screen = "blinker_right", audio = "none" },
1354     resource_class  = "player",
1355     screen_priority = 30
1356 }
1357
1358 application {
1359     appid           = "net.zmap.navi",
1360     area            = "Center.Full",
1361     privileges      = { screen = "none", audio = "none" },
1362     resource_class  = "navigator",
1363     screen_priority = 30
1364 }
1365
1366 application {
1367     appid           = "GV3ySIINq7.GhostCluster",
1368     area            = "Mid.Full",
1369     privileges      = { screen = "none", audio = "none" },
1370     resource_class  = "system",
1371     screen_priority = 30
1372 }
1373
1374 if sc then
1375     sc.client_handler = function (self, cid, msg)
1376         local command = msg.command
1377         if verbose > 0 then
1378             print('### ==> client handler:')
1379             if verbose > 1 then
1380                 print(msg)
1381             end
1382         end
1383         if not connected then
1384             print('Setting homescreen='..msg.appid)
1385             homescreen = msg.appid
1386             print('Trying to connect to wayland...')
1387             connected = wmgr:connect()
1388         end
1389         if connected and command then
1390             if command == 1 then -- send_appid
1391                 local driving_mode = mdb.select.select_driving_mode.single_value
1392                 local night_mode = mdb.select.select_night_mode.single_value
1393
1394                 if not driving_mode then driving_mode = 0 end
1395                 if not night_mode then night_mode = 0 end
1396
1397                 local reply = m:JSON({ command = 0x60001,
1398                                        arg     = m:JSON({ stateid = 1,
1399                                                           state   = driving_mode})
1400                                      })
1401                  if verbose > 0 then
1402                      print("### <== sending " ..
1403                            command_name(command) .. " message")
1404                      if verbose > 1 then
1405                          print(reply)
1406                      end
1407                  end
1408                  sc:send_message(homescreen, reply)
1409
1410                  reply = m:JSON({ command = 0x60001,
1411                                   arg     = m:JSON({ stateid = 2,
1412                                                      state   = night_mode})
1413                                 })
1414                  if verbose > 0 then
1415                      print("### <== sending " ..
1416                            command_name(command) .. " message")
1417                      if verbose > 1 then
1418                          print(reply)
1419                      end
1420                  end
1421                  sc:send_message(homescreen, reply)
1422             end
1423         end
1424     end
1425
1426     sc.generic_handler = function (self, cid, msg)
1427         if verbose > 0 then
1428             print('### ==> generic handler:')
1429             if verbose > 1 then
1430                print(msg)
1431             end
1432         end
1433     end
1434
1435     sc.window_handler = function (self, cid, msg)
1436         if verbose > 0 then
1437             print('### ==> received ' ..
1438                    command_name(msg.command) .. ' message')
1439             if verbose > 1 then
1440                 print(tostring(msg))
1441             end
1442         end
1443
1444         local a = animation({})
1445         local nores = false
1446         if msg.command == 0x10003 then       -- ico SHOW command
1447             local raise_mask = 0x01000000
1448             local lower_mask = 0x02000000
1449             local nores_mask = 0x40000000
1450             local time_mask  = 0x00ffffff
1451             msg.arg.visible = 1
1452             if msg.arg then
1453                 local time = 200
1454                 if  msg.arg.anim_time then
1455                     local t = msg.arg.anim_time
1456                     time = m:AND(t, time_mask)
1457                     nores = not m:AND(t, nores_mask)
1458                     if m:AND(t, raise_mask) then
1459                         msg.arg.raise = 1
1460                     elseif m:AND(t, lower_mask) then
1461                         msg.arg.raise = 0
1462                     end
1463                 end
1464                 if msg.arg.anim_name then
1465                     a.show = { msg.arg.anim_name, time }
1466                     print('time: ' .. tostring(a.show[2]))
1467                 end
1468             end
1469             if not nores then
1470                 local p = wmgr:application(msg.appid)
1471                 local s = p.privileges.screen
1472                 if s == "system" then
1473                     nores = true
1474                     if not msg.arg.raise then
1475                         msg.arg.raise = 1
1476                     end
1477                 end
1478             end
1479             if verbose > 2 then
1480                 print('### ==> SHOW')
1481                 print(tostring(msg.arg))
1482             end
1483             if nores then
1484                 wmgr:window_request(msg.arg, a, 0)
1485             else
1486                 local surface = msg.arg.surface
1487                 resclnt:resource_set_acquire("screen", surface)
1488                 resmgr:window_raise(msg.appid, surface, 1)
1489             end
1490         elseif msg.command == 0x10004 then   -- ico HIDE command
1491             local raise_mask = 0x01000000
1492             local lower_mask = 0x02000000
1493             local nores_mask = 0x40000000
1494             local time_mask  = 0x00ffffff
1495             msg.arg.visible = 0
1496             if msg.arg then
1497                 local time = 200
1498                 if msg.arg.anim_time then
1499                     local t = msg.arg.anim_time
1500                     time = m:AND(t, time_mask)
1501                     nores = not m:AND(t, nores_mask)
1502                 end
1503                 if msg.arg.anim_name then
1504                     a.hide = { msg.arg.anim_name, time }
1505                     print('hide animation time: ' .. tostring(a.hide[2]))
1506                 end
1507             end
1508             if not nores then
1509                 local p = wmgr:application(msg.appid)
1510                 local s = p.privileges.screen
1511                 if s == "system" then
1512                     nores = true
1513                     msg.arg.raise = 0
1514                 end
1515             end
1516             if verbose > 2 then
1517                 print('### ==> HIDE REQUEST')
1518                 print(tostring(msg.arg))
1519             end
1520             if nores then
1521                 wmgr:window_request(msg.arg, a, 0)
1522             else
1523                 resmgr:window_raise(msg.appid, msg.arg.surface, -1)
1524             end
1525         elseif msg.command == 0x10005 then   -- ico MOVE
1526             if verbose > 2 then
1527                 print('### ==> MOVE REQUEST')
1528                 print(tostring(msg.arg))
1529             end
1530             if msg.arg.zone then
1531                 msg.arg.area = msg.arg.zone
1532             end
1533             wmgr:window_request(msg.arg, a, 0)
1534             -- TODO: handle if area changed
1535         elseif msg.command == 0x10006 then   -- ico ACTIVE
1536             if not msg.arg.active then
1537                 msg.arg.active = 3 -- pointer + keyboard
1538             end
1539             if verbose > 2 then
1540                 print('### ==> ACTIVE REQUEST')
1541                 print(tostring(msg.arg))
1542             end
1543             wmgr:window_request(msg.arg, a, 0)
1544         elseif msg.command == 0x10007 then   -- ico CHANGE_LAYER
1545             if verbose > 2 then
1546                 print('### ==> CHANGE_LAYER REQUEST')
1547                 print(tostring(msg.arg))
1548             end
1549             --[[
1550             if msg.arg.layer ~= 4 or msg.arg.layer ~= 5 then
1551                 print("do not change layer for other than cursor or touch")
1552                 return
1553             end
1554             --]]
1555             wmgr:window_request(msg.arg, a, 0)
1556         elseif msg.command == 0x10011 then   -- ico MAP_THUMB
1557             local framerate = msg.arg.framerate
1558             if not framerate or framerate < 0 then
1559                 framerate = 0
1560             end
1561             msg.arg.map = 1
1562             if verbose > 2 then
1563                 print('### ==> MAP_THUMB REQUEST')
1564                 print(msg.arg)
1565                 print('framerate: '..framerate)
1566             end
1567             wmgr:window_request(msg.arg, a, framerate)
1568         elseif msg.command == 0x10012 then   -- ico UNMAP_THUMB
1569             msg.arg.map = 0
1570             if verbose > 2 then
1571                 print('### ==> UNMAP_THUMB REQUEST')
1572                 print(msg.arg)
1573             end
1574             wmgr:window_request(msg.arg, a, 0)
1575         elseif msg.command == 0x10020 then   -- ico SHOW_LAYER command
1576             msg.arg.visible = 1
1577             if verbose > 2 then
1578                 print('### ==> SHOW_LAYER REQUEST')
1579                 print(msg.arg)
1580             end
1581             wmgr:layer_request(msg.arg)
1582         elseif msg.command == 0x10021 then   -- ico HIDE_LAYER command
1583             msg.arg.visible = 0
1584             if verbose > 2 then
1585                 print('### ==> HIDE_LAYER REQUEST')
1586                 print(msg.arg)
1587             end
1588             wmgr:layer_request(msg.arg)
1589         end
1590     end
1591
1592     sc.input_handler = function (self, cid, msg)
1593         if verbose > 0 then
1594             print('### ==> input handler: ' .. command_name(msg.command))
1595             if verbose > 1 then
1596                 print(msg)
1597             end
1598         end
1599         if msg.command == 0x20001 then -- add_input
1600             msg.arg.appid = msg.appid
1601             if verbose > 2 then
1602                 print('### ==> ADD_INPUT REQUEST')
1603                 print(tostring(msg.arg))
1604             end
1605             imgr:input_request(msg.arg)
1606         elseif msg.command == 0x20002 then -- del_input
1607             msg.arg.appid = ''
1608             if verbose > 2 then
1609                 print('### ==> DEL_INPUT REQUEST')
1610                 print(tostring(msg.arg))
1611             end
1612             imgr:input_request(msg.arg)
1613         elseif msg.command == 0x20003 then -- send_input
1614         end
1615     end
1616
1617     sc.user_handler = function (self, cid, msg)
1618         if verbose > 0 then
1619             print('### ==> user handler: ' .. command_name(msg.command))
1620             if verbose > 1 then
1621                 print(msg)
1622             end
1623         end
1624
1625         if not um then
1626             print("User Manager not initialized")
1627             return
1628         end
1629
1630         if msg.command == 0x00030001 then -- MSG_CMD_CHANGE_USER
1631             print("command CHANGE_USER")
1632             if not msg.arg then
1633                 print("invalid message")
1634                 return
1635             end
1636
1637             username = msg.arg.user
1638             passwd = msg.arg.pass
1639
1640             if not username then
1641                 username = ""
1642             end
1643
1644             if not passwd then
1645                 passwd = ""
1646             end
1647
1648             success = um:changeUser(username, passwd)
1649
1650             if not success then
1651                 reply = m.JSON({
1652                     appid = msg.appid,
1653                     command = cmd
1654                 })
1655                 if sc:send_message(msg.appid, reply) then
1656                     print('*** sent authentication failed message')
1657                 else
1658                     print('*** failed to send authentication failed message')
1659                 end
1660             end
1661
1662         elseif msg.command == 0x00030002 then -- MSG_CMD_GET_USERLIST
1663             print("command GET_USERLIST")
1664             if not msg.appid then
1665                 print("invalid message")
1666                 return
1667             end
1668
1669             users, currentUser = um:getUserList()
1670
1671             if not users then
1672                 print("failed to get user list")
1673                 return
1674             end
1675
1676             nUsers = 0
1677
1678             for i,v in pairs(users) do
1679                 nUsers = nUsers + 1
1680             end
1681
1682             if not currentUser then
1683                 currentUser = ""
1684             end
1685
1686             if verbose > 1 then
1687                 print("current user: " .. currentUser)
1688                 print("user list:")
1689                 for i,v in pairs(users) do
1690                     print(v)
1691                 end
1692             end
1693
1694             reply = m.JSON({
1695                 appid = msg.appid,
1696                 command = cmd,
1697                 arg = m.JSON({
1698                     user_num = nUsers,
1699                     user_list = users,
1700                     user_login = currentUser
1701                 })
1702             })
1703
1704             if verbose > 1 then
1705                 print("### <== GetUserList reply: " .. tostring(reply))
1706             end
1707
1708             if sc:send_message(msg.appid, reply) then
1709                 print('*** reply OK')
1710             else
1711                 print('*** reply FAILED')
1712             end
1713
1714         elseif msg.command == 0x00030003 then -- MSG_CMD_GET_LASTINFO
1715             print("command GET_LASTINFO")
1716             if not msg.appid then
1717                 print("invalid message")
1718                 return
1719             end
1720
1721             lastInfo = um:getLastInfo(msg.appid)
1722
1723             if not lastInfo then
1724                 print("failed to get last info for app" .. msg.appid)
1725                 return
1726             end
1727
1728             reply = m.JSON({
1729                 appid = msg.appid,
1730                 command = cmd,
1731                 arg = m.JSON({
1732                     lastinfo = lastinfo
1733                 })
1734             })
1735
1736             if sc:send_message(msg.appid, reply) then
1737                 print('*** reply OK')
1738             else
1739                 print('*** reply FAILED')
1740             end
1741
1742         elseif msg.command == 0x00030004 then -- MSG_CMD_SET_LASTINFO
1743             print("command SET_LASTINFO")
1744             if not msg.arg or not msg.appid then
1745                 print("invalid message")
1746                 return
1747             end
1748
1749             lastInfo = um:setLastInfo(msg.appid, msg.arg.lastinfo)
1750         end
1751     end
1752
1753     sc.resource_handler = function (self, cid, msg)
1754         if verbose > 0 then
1755             print('### ==> resource handler: ' .. command_name(msg.command))
1756             if verbose > 1 then
1757                 print(msg)
1758             end
1759         end
1760
1761         createResourceSet = function (ctl, client, msg)
1762             cb = function(rset, data)
1763                 print("> resource callback")
1764
1765                 -- type is either basic (0) or interrupt (1)
1766                 requestType = 0
1767                 if msg.res.type then
1768                     requestType = msg.res.type
1769                 end
1770
1771                 if rset.acquired then
1772                     cmd = 0x00040001 -- acquire
1773                 else
1774                     cmd = 0x00040002 -- release
1775                 end
1776
1777                 reply = m.JSON({
1778                         appid = data.client,
1779                         command = cmd,
1780                         res = {
1781                             type = requestType
1782                         }
1783                     })
1784
1785                 if rset.resources.audio_playback then
1786                     reply.res.sound = {
1787                         zone = "driver",
1788                         name = msg.appid,
1789                         adjust = 0,
1790                         -- id = "0"
1791                     }
1792                 end
1793
1794                 if rset.resources.display then
1795                     reply.res.window = {
1796                         zone = "driver",
1797                         name = msg.appid,
1798                         -- id = "0"
1799                     }
1800                 end
1801
1802                 if rset.resources.input then
1803                     reply.res.input = {
1804                         name = msg.appid,
1805                         event = 0
1806                     }
1807                 end
1808                 print("sending message to client: " .. data.client)
1809
1810                 if sc:send_message(data.client, reply) then
1811                     print('*** reply OK')
1812                 else
1813                     print('*** reply FAILED')
1814                 end
1815             end
1816
1817             rset = m:ResourceSet({
1818                     application_class = "player",
1819                     zone = "driver", -- msg.zone ("full")
1820                     callback = cb
1821                 })
1822
1823             rset.data = {
1824                 cid = cid,
1825                 ctl = ctl
1826             }
1827
1828             if msg.res.sound then
1829                 rset:addResource({
1830                         resource_name = "audio_playback"
1831                     })
1832                 rset.resources.audio_playback.attributes.pid = tostring(msg.pid)
1833                 rset.resources.audio_playback.attributes.appid = msg.appid
1834                 print("sound name: " .. msg.res.sound.name)
1835                 print("sound zone:" .. msg.res.sound.zone)
1836                 print("sound adjust: " .. tostring(msg.res.sound.adjust))
1837                 if msg.res.sound.id then
1838                     print("sound id: " .. msg.res.sound.id)
1839                 end
1840             end
1841
1842             if msg.res.input then
1843                 rset:addResource({
1844                         resource_name = "input"
1845                     })
1846                 rset.resources.input.attributes.pid = tostring(msg.pid)
1847                 rset.resources.input.attributes.appid = msg.appid
1848                 print("input name: " .. msg.res.sound.name)
1849                 print("input event:" .. tostring(msg.res.input.event))
1850             end
1851
1852             if msg.res.window then
1853                 rset:addResource({
1854                         resource_name = "display"
1855                     })
1856                 rset.resources.display.attributes.pid = tostring(msg.pid)
1857                 rset.resources.display.attributes.appid = msg.appid
1858                 print("display name: " .. msg.res.display.name)
1859                 print("display zone:" .. msg.res.display.zone)
1860                 if msg.res.display.id then
1861                     print("display id: " .. msg.res.display.id)
1862                 end
1863             end
1864
1865             return rset
1866         end
1867
1868         -- parse the message
1869
1870         -- fields common to all messages:
1871         --      msg.command
1872         --      msg.appid
1873         --      msg.pid
1874
1875         if msg.command == 0x00040011 then -- MSG_CMD_CREATE_RES
1876             print("command CREATE")
1877
1878             if not sets.cid then
1879                 sets.cid = createResourceSet(self, cid, msg)
1880             end
1881
1882         elseif msg.command == 0x00040012 then -- MSG_CMD_DESTORY_RES
1883             print("command DESTROY")
1884
1885             if sets.cid then
1886                 sets.cid:release()
1887             end
1888
1889             sets.cid = nil -- garbage collecting
1890
1891         elseif msg.command == 0x00040001 then -- MSG_CMD_ACQUIRE_RES
1892             print("command ACQUIRE")
1893
1894             if not sets.cid then
1895                 sets.cid = createResourceSet(self, cid, msg)
1896             end
1897
1898             sets.cid:acquire()
1899
1900         elseif msg.command == 0x00040002 then -- MSG_CMD_RELEASE_RES
1901             print("command RELEASE")
1902
1903             if sets.cid then
1904                 sets.cid:release()
1905             end
1906
1907         elseif msg.command == 0x00040003 then -- MSG_CMD_DEPRIVE_RES
1908             print("command DEPRIVE")
1909
1910         elseif msg.command == 0x00040004 then -- MSG_CMD_WAITING_RES
1911             print("command WAITING")
1912
1913         elseif msg.command == 0x00040005 then -- MSG_CMD_REVERT_RES
1914             print("command REVERT")
1915         end
1916     end
1917
1918     sc.inputdev_handler = function (self, cid, msg)
1919         if verbose > 0 then
1920             print('*** inputdev handler: ' .. command_name(msg.command))
1921             if verbose > 1 then
1922                 print(msg)
1923             end
1924         end
1925     end
1926 end